Skip to content

πŸ™Œ OpenHands: AI-Driven Development

APP-1360 Removed V0 memory package (#14057)

Tim O'Farrell7498353ed58c
diff --git a/dev_config/python/mypy.ini b/dev_config/python/mypy.ini
index 0be43bc8ce..b2e656e508 100644
--- a/dev_config/python/mypy.ini
+++ b/dev_config/python/mypy.ini
@@ -12,9 +12,6 @@ disable_error_code = type-abstract
# Exclude third-party runtime directory from type checking
exclude = (third_party/|enterprise/)
-[mypy-openhands.memory.condenser.impl.*]
-disable_error_code = override
-
[mypy-openai.*]
follow_imports = skip
ignore_missing_imports = True
diff --git a/openhands/controller/state/state.py b/openhands/controller/state/state.py
index 5640f3ecb5..23299d72fb 100644
--- a/openhands/controller/state/state.py
+++ b/openhands/controller/state/state.py
@@ -19,6 +19,7 @@ from openhands.controller.state.control_flags import (
BudgetControlFlag,
IterationControlFlag,
)
+from openhands.controller.state.view import View
from openhands.core.logger import openhands_logger as logger
from openhands.core.schema import AgentState
from openhands.events.action import (
@@ -27,7 +28,6 @@ from openhands.events.action import (
from openhands.events.action.agent import AgentFinishAction
from openhands.events.event import Event, EventSource
from openhands.llm.metrics import Metrics
-from openhands.memory.view import View
from openhands.server.services.conversation_stats import ConversationStats
from openhands.storage.files import FileStore
from openhands.storage.locations import get_conversation_agent_state_filename
diff --git a/openhands/memory/view.py b/openhands/controller/state/view.py
similarity index 100%
rename from openhands/memory/view.py
rename to openhands/controller/state/view.py
diff --git a/openhands/memory/README.md b/openhands/memory/README.md
deleted file mode 100644
index 27abdb06e8..0000000000
--- a/openhands/memory/README.md
+++ /dev/null
@@ -1,18 +0,0 @@
-# Memory Component
-
-- Short Term History
-- Memory Condenser
-
-## Short Term History
-- Short term history filters the event stream and computes the messages that are injected into the context
-- It filters out certain events of no interest for the Agent, such as AgentChangeStateObservation or NullAction/NullObservation
-- When the context window or the token limit set by the user is exceeded, history starts condensing: chunks of messages into summaries.
-- Each summary is then injected into the context, in the place of the respective chunk it summarizes
-
-## Memory Condenser
-- Memory condenser is responsible for summarizing the chunks of events
-- It summarizes the earlier events first
-- It starts with the earliest agent actions and observations between two user messages
-- Then it does the same for later chunks of events between user messages
-- If there are no more agent events, it summarizes the user messages, this time one by one, if they're large enough and not immediately after an AgentFinishAction event (we assume those are tasks, potentially important)
-- Summaries are retrieved from the LLM as AgentSummarizeAction, and are saved in State.
diff --git a/openhands/memory/__init__.py b/openhands/memory/__init__.py
deleted file mode 100644
index e69de29bb2..0000000000
diff --git a/openhands/memory/condenser/__init__.py b/openhands/memory/condenser/__init__.py
deleted file mode 100644
index 8c20a2b257..0000000000
--- a/openhands/memory/condenser/__init__.py
+++ /dev/null
@@ -1,15 +0,0 @@
-import openhands.memory.condenser.impl # noqa F401 (we import this to get the condensers registered)
-from openhands.memory.condenser.condenser import (
- Condenser,
- get_condensation_metadata,
- View,
- Condensation,
-)
-
-__all__ = [
- 'Condenser',
- 'get_condensation_metadata',
- 'CONDENSER_REGISTRY',
- 'View',
- 'Condensation',
-]
diff --git a/openhands/memory/condenser/condenser.py b/openhands/memory/condenser/condenser.py
deleted file mode 100644
index c4e42277a9..0000000000
--- a/openhands/memory/condenser/condenser.py
+++ /dev/null
@@ -1,193 +0,0 @@
-from __future__ import annotations
-
-from abc import ABC, abstractmethod
-from contextlib import contextmanager
-from typing import Any
-
-from pydantic import BaseModel
-
-from openhands.controller.state.state import State
-from openhands.core.config.condenser_config import CondenserConfig
-from openhands.core.logger import openhands_logger as logger
-from openhands.events.action.agent import CondensationAction
-from openhands.llm.llm_registry import LLMRegistry
-from openhands.memory.view import View
-
-CONDENSER_METADATA_KEY = 'condenser_meta'
-"""Key identifying where metadata is stored in a `State` object's `extra_data` field."""
-
-
-def get_condensation_metadata(state: State) -> list[dict[str, Any]]:
- """Utility function to retrieve a list of metadata batches from a `State`.
-
- Args:
- state: The state to retrieve metadata from.
-
- Returns:
- list[dict[str, Any]]: A list of metadata batches, each representing a condensation.
- """
- if CONDENSER_METADATA_KEY in state.extra_data:
- return state.extra_data[CONDENSER_METADATA_KEY]
- return []
-
-
-CONDENSER_REGISTRY: dict[type[CondenserConfig], type[Condenser]] = {}
-"""Registry of condenser configurations to their corresponding condenser classes."""
-
-
-class Condensation(BaseModel):
- """Produced by a condenser to indicate the history has been condensed."""
-
- action: CondensationAction
-
-
-class Condenser(ABC):
- """Abstract condenser interface.
-
- Condensers take a list of `Event` objects and reduce them into a potentially smaller list.
-
- Agents can use condensers to reduce the amount of events they need to consider when deciding which action to take. To use a condenser, agents can call the `condensed_history` method on the current `State` being considered and use the results instead of the full history.
-
- If the condenser returns a `Condensation` instead of a `View`, the agent should return `Condensation.action` instead of producing its own action. On the next agent step the condenser will use that condensation event to produce a new `View`.
- """
-
- def __init__(self):
- self._metadata_batch: dict[str, Any] = {}
- self._llm_metadata: dict[str, Any] = {}
-
- def add_metadata(self, key: str, value: Any) -> None:
- """Add information to the current metadata batch.
-
- Any key/value pairs added to the metadata batch will be recorded in the `State` at the end of the current condensation.
-
- Args:
- key: The key to store the metadata under.
-
- value: The metadata to store.
- """
- self._metadata_batch[key] = value
-
- def write_metadata(self, state: State) -> None:
- """Write the current batch of metadata to the `State`.
-
- Resets the current metadata batch: any metadata added after this call will be stored in a new batch and written to the `State` at the end of the next condensation.
- """
- if CONDENSER_METADATA_KEY not in state.extra_data:
- state.extra_data[CONDENSER_METADATA_KEY] = []
- if self._metadata_batch:
- state.extra_data[CONDENSER_METADATA_KEY].append(self._metadata_batch)
-
- # Since the batch has been written, clear it for the next condensation
- self._metadata_batch = {}
-
- @contextmanager
- def metadata_batch(self, state: State):
- """Context manager to ensure batched metadata is always written to the `State`."""
- try:
- yield
- finally:
- self.write_metadata(state)
-
- @abstractmethod
- def condense(self, View) -> View | Condensation:
- """Condense a sequence of events into a potentially smaller list.
-
- New condenser strategies should override this method to implement their own condensation logic. Call `self.add_metadata` in the implementation to record any relevant per-condensation diagnostic information.
-
- Args:
- View: A view of the history containing all events that should be condensed.
-
- Returns:
- View | Condensation: A condensed view of the events or an event indicating the history has been condensed.
- """
-
- def condensed_history(self, state: State) -> View | Condensation:
- """Condense the state's history."""
- if hasattr(self, 'llm'):
- model_name = self.llm.config.model
- else:
- model_name = 'unknown'
-
- self._llm_metadata = state.to_llm_metadata(
- model_name=model_name, agent_name='condenser'
- )
- with self.metadata_batch(state):
- return self.condense(state.view)
-
- @property
- def llm_metadata(self) -> dict[str, Any]:
- """Metadata to be passed to the LLM when using this condenser.
-
- This metadata is used to provide context about the condensation process and can be used by the LLM to understand how the history was condensed.
- """
- if not self._llm_metadata:
- logger.warning(
- 'LLM metadata is empty. Ensure to set it in the condenser implementation.'
- )
- return self._llm_metadata
-
- @classmethod
- def register_config(cls, configuration_type: type[CondenserConfig]) -> None:
- """Register a new condenser configuration type.
-
- Instances of registered configuration types can be passed to `from_config` to create instances of the corresponding condenser.
-
- Args:
- configuration_type: The type of configuration used to create instances of the condenser.
-
- Raises:
- ValueError: If the configuration type is already registered.
- """
- if configuration_type in CONDENSER_REGISTRY:
- raise ValueError(
- f'Condenser configuration {configuration_type} is already registered'
- )
- CONDENSER_REGISTRY[configuration_type] = cls
-
- @classmethod
- def from_config(
- cls, config: CondenserConfig, llm_registry: LLMRegistry
- ) -> Condenser:
- """Create a condenser from a configuration object.
-
- Args:
- config: Configuration for the condenser.
-
- Returns:
- Condenser: A condenser instance.
-
- Raises:
- ValueError: If the condenser type is not recognized.
- """
- try:
- condenser_class = CONDENSER_REGISTRY[type(config)]
- return condenser_class.from_config(config, llm_registry)
- except KeyError:
- raise ValueError(f'Unknown condenser config: {config}')
-
-
-class RollingCondenser(Condenser, ABC):
- """Base class for a specialized condenser strategy that applies condensation to a rolling history.
-
- The rolling history is generated by `View.from_events`, which analyzes all events in the history and produces a `View` object representing what will be sent to the LLM.
-
- If `should_condense` says so, the condenser is then responsible for generating a `Condensation` object from the `View` object. This will be added to the event history which should -- when given to `get_view` -- produce the condensed `View` to be passed to the LLM.
- """
-
- @abstractmethod
- def should_condense(self, view: View) -> bool:
- """Determine if a view should be condensed."""
-
- @abstractmethod
- def get_condensation(self, view: View) -> Condensation:
- """Get the condensation from a view."""
-
- def condense(self, view: View) -> View | Condensation:
- # If we trigger the condenser-specific condensation threshold, compute and return
- # the condensation.
- if self.should_condense(view):
- return self.get_condensation(view)
-
- # Otherwise we're safe to just return the view.
- else:
- return view
diff --git a/openhands/memory/condenser/impl/__init__.py b/openhands/memory/condenser/impl/__init__.py
deleted file mode 100644
index 5bb6084165..0000000000
--- a/openhands/memory/condenser/impl/__init__.py
+++ /dev/null
@@ -1,41 +0,0 @@
-from openhands.memory.condenser.impl.amortized_forgetting_condenser import (
- AmortizedForgettingCondenser,
-)
-from openhands.memory.condenser.impl.browser_output_condenser import (
- BrowserOutputCondenser,
-)
-from openhands.memory.condenser.impl.conversation_window_condenser import (
- ConversationWindowCondenser,
-)
-from openhands.memory.condenser.impl.llm_attention_condenser import (
- ImportantEventSelection,
- LLMAttentionCondenser,
-)
-from openhands.memory.condenser.impl.llm_summarizing_condenser import (
- LLMSummarizingCondenser,
-)
-from openhands.memory.condenser.impl.no_op_condenser import NoOpCondenser
-from openhands.memory.condenser.impl.observation_masking_condenser import (
- ObservationMaskingCondenser,
-)
-from openhands.memory.condenser.impl.pipeline import CondenserPipeline
-from openhands.memory.condenser.impl.recent_events_condenser import (
- RecentEventsCondenser,
-)
-from openhands.memory.condenser.impl.structured_summary_condenser import (
- StructuredSummaryCondenser,
-)
-
-__all__ = [
- 'AmortizedForgettingCondenser',
- 'LLMAttentionCondenser',
- 'ImportantEventSelection',
- 'LLMSummarizingCondenser',
- 'NoOpCondenser',
- 'ObservationMaskingCondenser',
- 'BrowserOutputCondenser',
- 'RecentEventsCondenser',
- 'StructuredSummaryCondenser',
- 'CondenserPipeline',
- 'ConversationWindowCondenser',
-]
diff --git a/openhands/memory/condenser/impl/amortized_forgetting_condenser.py b/openhands/memory/condenser/impl/amortized_forgetting_condenser.py
deleted file mode 100644
index 5981b4091b..0000000000
--- a/openhands/memory/condenser/impl/amortized_forgetting_condenser.py
+++ /dev/null
@@ -1,69 +0,0 @@
-from __future__ import annotations
-
-from openhands.core.config.condenser_config import AmortizedForgettingCondenserConfig
-from openhands.events.action.agent import CondensationAction
-from openhands.llm.llm_registry import LLMRegistry
-from openhands.memory.condenser.condenser import (
- Condensation,
- RollingCondenser,
- View,
-)
-
-
-class AmortizedForgettingCondenser(RollingCondenser):
- """A condenser that maintains a condensed history and forgets old events when it grows too large."""
-
- def __init__(self, max_size: int = 100, keep_first: int = 0):
- """Initialize the condenser.
-
- Args:
- max_size: Maximum size of history before forgetting.
- keep_first: Number of initial events to always keep.
-
- Raises:
- ValueError: If keep_first is greater than max_size, keep_first is negative, or max_size is non-positive.
- """
- if keep_first >= max_size // 2:
- raise ValueError(
- f'keep_first ({keep_first}) must be less than half of max_size ({max_size})'
- )
- if keep_first < 0:
- raise ValueError(f'keep_first ({keep_first}) cannot be negative')
- if max_size < 1:
- raise ValueError(f'max_size ({max_size}) cannot be non-positive')
-
- self.max_size = max_size
- self.keep_first = keep_first
-
- super().__init__()
-
- def get_condensation(self, view: View) -> Condensation:
- target_size = self.max_size // 2
- head = view[: self.keep_first]
-
- events_from_tail = target_size - len(head)
- tail = view[-events_from_tail:]
-
- event_ids_to_keep = {event.id for event in head + tail}
- event_ids_to_forget = {event.id for event in view} - event_ids_to_keep
-
- event = CondensationAction(
- forgotten_events_start_id=min(event_ids_to_forget),
- forgotten_events_end_id=max(event_ids_to_forget),
- )
-
- return Condensation(action=event)
-
- def should_condense(self, view: View) -> bool:
- return len(view) > self.max_size or view.unhandled_condensation_request
-
- @classmethod
- def from_config(
- cls,
- config: AmortizedForgettingCondenserConfig,
- llm_registry: LLMRegistry,
- ) -> AmortizedForgettingCondenser:
- return AmortizedForgettingCondenser(**config.model_dump(exclude={'type'}))
-
-
-AmortizedForgettingCondenser.register_config(AmortizedForgettingCondenserConfig)
diff --git a/openhands/memory/condenser/impl/browser_output_condenser.py b/openhands/memory/condenser/impl/browser_output_condenser.py
deleted file mode 100644
index b3e4683cad..0000000000
--- a/openhands/memory/condenser/impl/browser_output_condenser.py
+++ /dev/null
@@ -1,49 +0,0 @@
-from __future__ import annotations
-
-from openhands.core.config.condenser_config import BrowserOutputCondenserConfig
-from openhands.events.event import Event
-from openhands.events.observation import BrowserOutputObservation
-from openhands.events.observation.agent import AgentCondensationObservation
-from openhands.llm.llm_registry import LLMRegistry
-from openhands.memory.condenser.condenser import Condensation, Condenser, View
-
-
-class BrowserOutputCondenser(Condenser):
- """A condenser that masks the observations from browser outputs outside of a recent attention window.
-
- The intent here is to mask just the browser outputs and leave everything else untouched. This is important because currently we provide screenshots and accessibility trees as input to the model for browser observations. These are really large and consume a lot of tokens without any benefits in performance. So we want to mask all such observations from all previous timesteps, and leave only the most recent one in context.
- """
-
- def __init__(self, attention_window: int = 1):
- self.attention_window = attention_window
- super().__init__()
-
- def condense(self, view: View) -> View | Condensation:
- """Replace the content of browser observations outside of the attention window with a placeholder."""
- results: list[Event] = []
- cnt: int = 0
- for event in reversed(view):
- if (
- isinstance(event, BrowserOutputObservation)
- and cnt >= self.attention_window
- ):
- results.append(
- AgentCondensationObservation(
- f'Visited URL {event.url}\nContent omitted'
- )
- )
- else:
- results.append(event)
- if isinstance(event, BrowserOutputObservation):
- cnt += 1
-
- return View(events=list(reversed(results)))
-
- @classmethod
- def from_config(
- cls, config: BrowserOutputCondenserConfig, llm_registry: LLMRegistry
- ) -> BrowserOutputCondenser:
- return BrowserOutputCondenser(**config.model_dump(exclude={'type'}))
-
-
-BrowserOutputCondenser.register_config(BrowserOutputCondenserConfig)
diff --git a/openhands/memory/condenser/impl/conversation_window_condenser.py b/openhands/memory/condenser/impl/conversation_window_condenser.py
deleted file mode 100644
index 9ca9e11255..0000000000
--- a/openhands/memory/condenser/impl/conversation_window_condenser.py
+++ /dev/null
@@ -1,188 +0,0 @@
-from __future__ import annotations
-
-from openhands.core.config.condenser_config import ConversationWindowCondenserConfig
-from openhands.core.logger import openhands_logger as logger
-from openhands.events.action.agent import (
- CondensationAction,
- RecallAction,
-)
-from openhands.events.action.message import MessageAction, SystemMessageAction
-from openhands.events.event import EventSource
-from openhands.events.observation import Observation
-from openhands.llm.llm_registry import LLMRegistry
-from openhands.memory.condenser.condenser import Condensation, RollingCondenser, View
-
-
-class ConversationWindowCondenser(RollingCondenser):
- def __init__(self) -> None:
- super().__init__()
-
- def get_condensation(self, view: View) -> Condensation:
- """Apply conversation window truncation similar to _apply_conversation_window.
-
- This method:
- 1. Identifies essential initial events (System Message, First User Message, Recall Observation)
- 2. Keeps roughly half of the history
- 3. Ensures action-observation pairs are preserved
- 4. Returns a CondensationAction specifying which events to forget
- """
- events = view.events
-
- # Handle empty history
- if not events:
- # No events to condense
- action = CondensationAction(forgotten_event_ids=[])
- return Condensation(action=action)
-
- # 1. Identify essential initial events
- system_message: SystemMessageAction | None = None
- first_user_msg: MessageAction | None = None
- recall_action: RecallAction | None = None
- recall_observation: Observation | None = None
-
- # Find System Message (should be the first event, if it exists)
- system_message = next(
- (e for e in events if isinstance(e, SystemMessageAction)), None
- )
-
- # Find First User Message
- first_user_msg = next(
- (
- e
- for e in events
- if isinstance(e, MessageAction) and e.source == EventSource.USER
- ),
- None,
- )
-
- if first_user_msg is None:
- logger.warning(
- 'No first user message found in history during condensation.'
- )
- # Return empty condensation if no user message
- action = CondensationAction(forgotten_event_ids=[])
- return Condensation(action=action)
-
- # Find the first user message index
- first_user_msg_index = -1
- for i, event in enumerate(events):
- if isinstance(event, MessageAction) and event.source == EventSource.USER:
- first_user_msg_index = i
- break
-
- # Find Recall Action and Observation related to the First User Message
- for i in range(first_user_msg_index + 1, len(events)):
- event = events[i]
- if (
- isinstance(event, RecallAction)
- and event.query == first_user_msg.content
- ):
- recall_action = event
- # Look for its observation
- for j in range(i + 1, len(events)):
- obs_event = events[j]
- if (
- isinstance(obs_event, Observation)
- and obs_event.cause == recall_action.id
- ):
- recall_observation = obs_event
- break
- break
-
- # Collect essential events
- essential_events: list[int] = [] # Store event IDs
- if system_message:
- essential_events.append(system_message.id)
- essential_events.append(first_user_msg.id)
- if recall_action:
- essential_events.append(recall_action.id)
- if recall_observation:
- essential_events.append(recall_observation.id)
-
- # 2. Determine which events to keep
- num_essential_events = len(essential_events)
- total_events = len(events)
- num_non_essential_events = total_events - num_essential_events
-
- # Keep roughly half of the non-essential events
- num_recent_to_keep = max(1, num_non_essential_events // 2)
-
- # Calculate the starting index for recent events to keep
- slice_start_index = total_events - num_recent_to_keep
- slice_start_index = max(0, slice_start_index)
-
- # 3. Handle dangling observations at the start of the slice
- # Find the first non-observation event in the slice
- recent_events_slice = events[slice_start_index:]
- first_valid_event_index_in_slice = 0
- for i, event in enumerate(recent_events_slice):
- if not isinstance(event, Observation):
- first_valid_event_index_in_slice = i
- break
- else:
- # All events in the slice are observations
- first_valid_event_index_in_slice = len(recent_events_slice)
-
- # Check if all events in the recent slice are dangling observations
- if first_valid_event_index_in_slice == len(recent_events_slice):
- logger.warning(
- 'All recent events are dangling observations, which we truncate. This means the agent has only the essential first events. This should not happen.'
- )
-
- # Calculate the actual index in the full events list
- first_valid_event_index = slice_start_index + first_valid_event_index_in_slice
-
- if first_valid_event_index_in_slice > 0:
- logger.debug(
- f'Removed {first_valid_event_index_in_slice} dangling observation(s) '
- f'from the start of recent event slice.'
- )
-
- # 4. Determine which events to keep and which to forget
- events_to_keep: set[int] = set(essential_events)
-
- # Add recent events starting from first_valid_event_index
- for i in range(first_valid_event_index, total_events):
- events_to_keep.add(events[i].id)
-
- # Calculate which events to forget
- all_event_ids = {e.id for e in events}
- forgotten_event_ids = sorted(all_event_ids - events_to_keep)
-
- logger.info(
- f'ConversationWindowCondenser: Keeping {len(events_to_keep)} events, '
- f'forgetting {len(forgotten_event_ids)} events.'
- )
-
- # Create the condensation action
- if forgotten_event_ids:
- # Use range if the forgotten events are contiguous
- if (
- len(forgotten_event_ids) > 1
- and forgotten_event_ids[-1] - forgotten_event_ids[0]
- == len(forgotten_event_ids) - 1
- ):
- action = CondensationAction(
- forgotten_events_start_id=forgotten_event_ids[0],
- forgotten_events_end_id=forgotten_event_ids[-1],
- )
- else:
- action = CondensationAction(forgotten_event_ids=forgotten_event_ids)
- else:
- action = CondensationAction(forgotten_event_ids=[])
-
- return Condensation(action=action)
-
- def should_condense(self, view: View) -> bool:
- return view.unhandled_condensation_request
-
- @classmethod
- def from_config(
- cls,
- _config: ConversationWindowCondenserConfig,
- llm_registry: LLMRegistry,
- ) -> ConversationWindowCondenser:
- return ConversationWindowCondenser()
-
-
-ConversationWindowCondenser.register_config(ConversationWindowCondenserConfig)
diff --git a/openhands/memory/condenser/impl/llm_attention_condenser.py b/openhands/memory/condenser/impl/llm_attention_condenser.py
deleted file mode 100644
index 7ac3d06074..0000000000
--- a/openhands/memory/condenser/impl/llm_attention_condenser.py
+++ /dev/null
@@ -1,140 +0,0 @@
-from __future__ import annotations
-
-from litellm import supports_response_schema
-from pydantic import BaseModel
-
-from openhands.core.config.condenser_config import LLMAttentionCondenserConfig
-from openhands.events.action.agent import CondensationAction
-from openhands.llm.llm import LLM
-from openhands.llm.llm_registry import LLMRegistry
-from openhands.memory.condenser.condenser import (
- Condensation,
- RollingCondenser,
- View,
-)
-
-
-class ImportantEventSelection(BaseModel):
- """Utility class for the `LLMAttentionCondenser` that forces the LLM to return a list of integers."""
-
- ids: list[int]
-
-
-class LLMAttentionCondenser(RollingCondenser):
- """Rolling condenser strategy that uses an LLM to select the most important events when condensing the history."""
-
- def __init__(
- self,
- llm: LLM,
- max_size: int = 100,
- keep_first: int = 1,
- ):
- if keep_first >= max_size // 2:
- raise ValueError(
- f'keep_first ({keep_first}) must be less than half of max_size ({max_size})'
- )
- if keep_first < 0:
- raise ValueError(f'keep_first ({keep_first}) cannot be negative')
- if max_size < 1:
- raise ValueError(f'max_size ({max_size}) cannot be non-positive')
-
- self.max_size = max_size
- self.keep_first = keep_first
- self.llm = llm
-
- # This condenser relies on the `response_schema` feature, which is not supported by all LLMs
- if not supports_response_schema(
- model=self.llm.config.model,
- custom_llm_provider=self.llm.config.custom_llm_provider,
- ):
- raise ValueError(
- "The LLM model must support the 'response_schema' parameter to use the LLMAttentionCondenser."
- )
-
- super().__init__()
-
- def get_condensation(self, view: View) -> Condensation:
- target_size = self.max_size // 2
- head_event_ids = [event.id for event in view.events[: self.keep_first]]
-
- events_from_tail = target_size - len(head_event_ids)
-
- message: str = """You will be given a list of actions, observations, and thoughts from a coding agent.
- Each item in the list has an identifier. Please sort the identifiers in order of how important the
- contents of the item are for the next step of the coding agent's task, from most important to least
- important."""
-
- response = self.llm.completion(
- messages=[
- {'content': message, 'role': 'user'},
- *[
- {
- 'content': f'<ID>{e.id}</ID>\n<CONTENT>{e.message}</CONTENT>',
- 'role': 'user',
- }
- for e in view
- ],
- ],
- response_format={
- 'type': 'json_schema',
- 'json_schema': {
- 'name': 'ImportantEventSelection',
- 'schema': ImportantEventSelection.model_json_schema(),
- },
- },
- )
-
- response_ids = ImportantEventSelection.model_validate_json(
- response.choices[0].message.content
- ).ids
-
- self.add_metadata('metrics', self.llm.metrics.get())
-
- # Filter out any IDs from the head and trim the results down
- response_ids = [
- response_id
- for response_id in response_ids
- if response_id not in head_event_ids
- ][:events_from_tail]
-
- # If the response IDs aren't _long_ enough, iterate backwards through the events and add any unfound IDs to the list.
- for event in reversed(view):
- if len(response_ids) >= events_from_tail:
- break
- if event.id not in response_ids:
- response_ids.append(event.id)
-
- # Now that we've found the right number of events to keep, convert this into a list of events to forget.
- event = CondensationAction(
- forgotten_event_ids=[
- event.id
- for event in view
- if event.id not in response_ids and event.id not in head_event_ids
- ],
- )
-
- return Condensation(action=event)
-
- def should_condense(self, view: View) -> bool:
- return len(view) > self.max_size or view.unhandled_condensation_request
-
- @classmethod
- def from_config(
- cls, config: LLMAttentionCondenserConfig, llm_registry: LLMRegistry
- ) -> LLMAttentionCondenser:
- # This condenser cannot take advantage of prompt caching. If it happens
- # to be set, we'll pay for the cache writes but never get a chance to
- # save on a read.
- llm_config = config.llm_config.model_copy()
- llm_config.caching_prompt = False
-
- llm = llm_registry.get_llm('condenser', llm_config)
-
- return LLMAttentionCondenser(
- llm=llm,
- max_size=config.max_size,
- keep_first=config.keep_first,
- )
-
-
-LLMAttentionCondenser.register_config(LLMAttentionCondenserConfig)
diff --git a/openhands/memory/condenser/impl/llm_summarizing_condenser.py b/openhands/memory/condenser/impl/llm_summarizing_condenser.py
deleted file mode 100644
index c6553ca6c0..0000000000
--- a/openhands/memory/condenser/impl/llm_summarizing_condenser.py
+++ /dev/null
@@ -1,182 +0,0 @@
-from __future__ import annotations
-
-from openhands.core.config.condenser_config import LLMSummarizingCondenserConfig
-from openhands.core.message import Message, TextContent
-from openhands.events.action.agent import CondensationAction
-from openhands.events.observation.agent import AgentCondensationObservation
-from openhands.events.serialization.event import truncate_content
-from openhands.llm.llm import LLM
-from openhands.llm.llm_registry import LLMRegistry
-from openhands.memory.condenser.condenser import (
- Condensation,
- RollingCondenser,
- View,
-)
-
-
-class LLMSummarizingCondenser(RollingCondenser):
- """A condenser that summarizes forgotten events.
-
- Maintains a condensed history and forgets old events when it grows too large,
- keeping a special summarization event after the prefix that summarizes all previous summarizations
- and newly forgotten events.
- """
-
- def __init__(
- self,
- llm: LLM,
- max_size: int = 100,
- keep_first: int = 1,
- max_event_length: int = 10_000,
- ):
- if keep_first >= max_size // 2:
- raise ValueError(
- f'keep_first ({keep_first}) must be less than half of max_size ({max_size})'
- )
- if keep_first < 0:
- raise ValueError(f'keep_first ({keep_first}) cannot be negative')
- if max_size < 1:
- raise ValueError(f'max_size ({max_size}) cannot be non-positive')
-
- self.max_size = max_size
- self.keep_first = keep_first
- self.max_event_length = max_event_length
- self.llm = llm
-
- super().__init__()
-
- def _truncate(self, content: str) -> str:
- """Truncate the content to fit within the specified maximum event length."""
- return truncate_content(content, max_chars=self.max_event_length)
-
- def get_condensation(self, view: View) -> Condensation:
- head = view[: self.keep_first]
- target_size = self.max_size // 2
- # Number of events to keep from the tail -- target size, minus however many
- # prefix events from the head, minus one for the summarization event
- events_from_tail = target_size - len(head) - 1
-
- summary_event = (
- view[self.keep_first]
- if isinstance(view[self.keep_first], AgentCondensationObservation)
- else AgentCondensationObservation('No events summarized')
- )
-
- # Identify events to be forgotten (those not in head or tail)
- forgotten_events = []
- for event in view[self.keep_first : -events_from_tail]:
- if not isinstance(event, AgentCondensationObservation):
- forgotten_events.append(event)
-
- # Construct prompt for summarization
- prompt = """You are maintaining a context-aware state summary for an interactive agent.
-You will be given a list of events corresponding to actions taken by the agent, and the most recent previous summary if one exists.
-If the events being summarized contain ANY task-tracking, you MUST include a TASK_TRACKING section to maintain continuity.
-When referencing tasks make sure to preserve exact task IDs and statuses.
-
-Track:
-
-USER_CONTEXT: (Preserve essential user requirements, goals, and clarifications in concise form)
-
-TASK_TRACKING: {Active tasks, their IDs and statuses - PRESERVE TASK IDs}
-
-COMPLETED: (Tasks completed so far, with brief results)
-PENDING: (Tasks that still need to be done)
-CURRENT_STATE: (Current variables, data structures, or relevant state)
-
-For code-specific tasks, also include:
-CODE_STATE: {File paths, function signatures, data structures}
-TESTS: {Failing cases, error messages, outputs}
-CHANGES: {Code edits, variable updates}
-DEPS: {Dependencies, imports, external calls}
-VERSION_CONTROL_STATUS: {Repository state, current branch, PR status, commit history}
-
-PRIORITIZE:
-1. Adapt tracking format to match the actual task type
-2. Capture key user requirements and goals
-3. Distinguish between completed and pending tasks
-4. Keep all sections concise and relevant
-
-SKIP: Tracking irrelevant details for the current task type
-
-Example formats:
-
-For code tasks:
-USER_CONTEXT: Fix FITS card float representation issue
-COMPLETED: Modified mod_float() in card.py, all tests passing
-PENDING: Create PR, update documentation
-CODE_STATE: mod_float() in card.py updated
-TESTS: test_format() passed
-CHANGES: str(val) replaces f"{val:.16G}"
-DEPS: None modified
-VERSION_CONTROL_STATUS: Branch: fix-float-precision, Latest commit: a1b2c3d
-
-For other tasks:
-USER_CONTEXT: Write 20 haikus based on coin flip results
-COMPLETED: 15 haikus written for results [T,H,T,H,T,H,T,T,H,T,H,T,H,T,H]
-PENDING: 5 more haikus needed
-CURRENT_STATE: Last flip: Heads, Haiku count: 15/20"""
-
- prompt += '\n\n'
-
- # Add the previous summary if it exists. We'll always have a summary
- # event, but the types aren't precise enought to guarantee that it has a
- # message attribute.
- summary_event_content = self._truncate(
- summary_event.message if summary_event.message else ''
- )
- prompt += f'<PREVIOUS SUMMARY>\n{summary_event_content}\n</PREVIOUS SUMMARY>\n'
-
- prompt += '\n\n'
-
- # Add all events that are being forgotten. We use the string
- # representation defined by the event, and truncate it if necessary.
- for forgotten_event in forgotten_events:
- event_content = self._truncate(str(forgotten_event))
- prompt += f'<EVENT id={forgotten_event.id}>\n{event_content}\n</EVENT>\n'
-
- prompt += 'Now summarize the events using the rules above.'
-
- messages = [Message(role='user', content=[TextContent(text=prompt)])]
-
- response = self.llm.completion(
- messages=self.llm.format_messages_for_llm(messages),
- extra_body={'metadata': self.llm_metadata},
- )
- summary = response.choices[0].message.content
-
- self.add_metadata('response', response.model_dump())
- self.add_metadata('metrics', self.llm.metrics.get())
-
- return Condensation(
- action=CondensationAction(
- forgotten_events_start_id=min(event.id for event in forgotten_events),
- forgotten_events_end_id=max(event.id for event in forgotten_events),
- summary=summary,
- summary_offset=self.keep_first,
- )
- )
-
- def should_condense(self, view: View) -> bool:
- return len(view) > self.max_size or view.unhandled_condensation_request
-
- @classmethod
- def from_config(
- cls, config: LLMSummarizingCondenserConfig, llm_registry: LLMRegistry
- ) -> LLMSummarizingCondenser:
- # This condenser cannot take advantage of prompt caching. If it happens
- # to be set, we'll pay for the cache writes but never get a chance to
- # save on a read.
- llm_config = config.llm_config.model_copy()
- llm_config.caching_prompt = False
- llm = llm_registry.get_llm('condenser', llm_config)
-
- return LLMSummarizingCondenser(
- llm=llm,
- max_size=config.max_size,
- keep_first=config.keep_first,
- max_event_length=config.max_event_length,
- )
-
-
-LLMSummarizingCondenser.register_config(LLMSummarizingCondenserConfig)
diff --git a/openhands/memory/condenser/impl/no_op_condenser.py b/openhands/memory/condenser/impl/no_op_condenser.py
deleted file mode 100644
index 9f480a129f..0000000000
--- a/openhands/memory/condenser/impl/no_op_condenser.py
+++ /dev/null
@@ -1,22 +0,0 @@
-from __future__ import annotations
-
-from openhands.core.config.condenser_config import NoOpCondenserConfig
-from openhands.llm.llm_registry import LLMRegistry
-from openhands.memory.condenser.condenser import Condensation, Condenser, View
-
-
-class NoOpCondenser(Condenser):
- """A condenser that does nothing to the event sequence."""
-
- def condense(self, view: View) -> View | Condensation:
- """Returns the list of events unchanged."""
- return view
-
- @classmethod
- def from_config(
- cls, config: NoOpCondenserConfig, llm_registry: LLMRegistry
- ) -> NoOpCondenser:
- return NoOpCondenser()
-
-
-NoOpCondenser.register_config(NoOpCondenserConfig)
diff --git a/openhands/memory/condenser/impl/observation_masking_condenser.py b/openhands/memory/condenser/impl/observation_masking_condenser.py
deleted file mode 100644
index 71d691b00f..0000000000
--- a/openhands/memory/condenser/impl/observation_masking_condenser.py
+++ /dev/null
@@ -1,39 +0,0 @@
-from __future__ import annotations
-
-from openhands.core.config.condenser_config import ObservationMaskingCondenserConfig
-from openhands.events.event import Event
-from openhands.events.observation import Observation
-from openhands.events.observation.agent import AgentCondensationObservation
-from openhands.llm.llm_registry import LLMRegistry
-from openhands.memory.condenser.condenser import Condensation, Condenser, View
-
-
-class ObservationMaskingCondenser(Condenser):
- """A condenser that masks the values of observations outside of a recent attention window."""
-
- def __init__(self, attention_window: int = 5):
- self.attention_window = attention_window
-
- super().__init__()
-
- def condense(self, view: View) -> View | Condensation:
- """Replace the content of observations outside of the attention window with a placeholder."""
- results: list[Event] = []
- for i, event in enumerate(view):
- if isinstance(event, Observation) and i < len(view) - self.attention_window:
- results.append(AgentCondensationObservation('<MASKED>'))
- else:
- results.append(event)
-
- return View(events=results)
-
- @classmethod
- def from_config(
- cls,
- config: ObservationMaskingCondenserConfig,
- llm_registry: LLMRegistry,
- ) -> ObservationMaskingCondenser:
- return ObservationMaskingCondenser(**config.model_dump(exclude={'type'}))
-
-
-ObservationMaskingCondenser.register_config(ObservationMaskingCondenserConfig)
diff --git a/openhands/memory/condenser/impl/pipeline.py b/openhands/memory/condenser/impl/pipeline.py
deleted file mode 100644
index c32aa4b255..0000000000
--- a/openhands/memory/condenser/impl/pipeline.py
+++ /dev/null
@@ -1,50 +0,0 @@
-from __future__ import annotations
-
-from contextlib import contextmanager
-
-from openhands.controller.state.state import State
-from openhands.core.config.condenser_config import CondenserPipelineConfig
-from openhands.llm.llm_registry import LLMRegistry
-from openhands.memory.condenser.condenser import Condensation, Condenser
-from openhands.memory.view import View
-
-
-class CondenserPipeline(Condenser):
- """Combines multiple condensers into a single condenser.
-
- This is useful for creating a pipeline of condensers that can be chained together to achieve very specific condensation aims. Each condenser is run in sequence, passing the output view of one to the next, until we reach the end or a `CondensationAction` is returned instead.
- """
-
- def __init__(self, *condenser: Condenser) -> None:
- self.condensers = list(condenser)
- super().__init__()
-
- @contextmanager
- def metadata_batch(self, state: State):
- try:
- yield
- finally:
- # The parent class assumes the metadata is stored in the "calling
- # condenser" -- since we're not threading a State through to each
- # step in the pipeline, we need to walk back through the pipeline
- # and manually collect the relevant metadata.
- for condenser in self.condensers:
- condenser.write_metadata(state)
-
- def condense(self, view: View) -> View | Condensation:
- result: View | Condensation = view
- for condenser in self.condensers:
- result = condenser.condense(result)
- if isinstance(result, Condensation):
- break
- return result
-
- @classmethod
- def from_config(
- cls, config: CondenserPipelineConfig, llm_registry: LLMRegistry
- ) -> CondenserPipeline:
- condensers = [Condenser.from_config(c, llm_registry) for c in config.condensers]
- return CondenserPipeline(*condensers)
-
-
-CondenserPipeline.register_config(CondenserPipelineConfig)
diff --git a/openhands/memory/condenser/impl/recent_events_condenser.py b/openhands/memory/condenser/impl/recent_events_condenser.py
deleted file mode 100644
index 9feed0aea2..0000000000
--- a/openhands/memory/condenser/impl/recent_events_condenser.py
+++ /dev/null
@@ -1,31 +0,0 @@
-from __future__ import annotations
-
-from openhands.core.config.condenser_config import RecentEventsCondenserConfig
-from openhands.llm.llm_registry import LLMRegistry
-from openhands.memory.condenser.condenser import Condensation, Condenser, View
-
-
-class RecentEventsCondenser(Condenser):
- """A condenser that only keeps a certain number of the most recent events."""
-
- def __init__(self, keep_first: int = 1, max_events: int = 10):
- self.keep_first = keep_first
- self.max_events = max_events
-
- super().__init__()
-
- def condense(self, view: View) -> View | Condensation:
- """Keep only the most recent events (up to `max_events`)."""
- head = view[: self.keep_first]
- tail_length = max(0, self.max_events - len(head))
- tail = view[-tail_length:] if tail_length > 0 else []
- return View(events=head + tail)
-
- @classmethod
- def from_config(
- cls, config: RecentEventsCondenserConfig, llm_registry: LLMRegistry
- ) -> RecentEventsCondenser:
- return RecentEventsCondenser(**config.model_dump(exclude={'type'}))
-
-
-RecentEventsCondenser.register_config(RecentEventsCondenserConfig)
diff --git a/openhands/memory/condenser/impl/structured_summary_condenser.py b/openhands/memory/condenser/impl/structured_summary_condenser.py
deleted file mode 100644
index f06ae17a2c..0000000000
--- a/openhands/memory/condenser/impl/structured_summary_condenser.py
+++ /dev/null
@@ -1,329 +0,0 @@
-from __future__ import annotations
-
-import json
-from typing import Any
-
-from pydantic import BaseModel, Field
-
-from openhands.core.config.condenser_config import (
- StructuredSummaryCondenserConfig,
-)
-from openhands.core.logger import openhands_logger as logger
-from openhands.core.message import Message, TextContent
-from openhands.events.action.agent import CondensationAction
-from openhands.events.observation.agent import AgentCondensationObservation
-from openhands.events.serialization.event import truncate_content
-from openhands.llm.llm import LLM
-from openhands.llm.llm_registry import LLMRegistry
-from openhands.memory.condenser.condenser import (
- Condensation,
- RollingCondenser,
- View,
-)
-
-
-class StateSummary(BaseModel):
- """A structured representation summarizing the state of the agent and the task."""
-
- # Required core fields
- user_context: str = Field(
- default='',
- description='Essential user requirements, goals, and clarifications in concise form.',
- )
- completed_tasks: str = Field(
- default='', description='List of tasks completed so far with brief results.'
- )
- pending_tasks: str = Field(
- default='', description='List of tasks that still need to be done.'
- )
- current_state: str = Field(
- default='',
- description='Current variables, data structures, or other relevant state information.',
- )
-
- # Code state fields
- files_modified: str = Field(
- default='', description='List of files that have been created or modified.'
- )
- function_changes: str = Field(
- default='', description='List of functions that have been created or modified.'
- )
- data_structures: str = Field(
- default='', description='List of key data structures in use or modified.'
- )
-
- # Test status fields
- tests_written: str = Field(
- default='',
- description='Whether tests have been written for the changes. True, false, or unknown.',
- )
- tests_passing: str = Field(
- default='',
- description='Whether all tests are currently passing. True, false, or unknown.',
- )
- failing_tests: str = Field(
- default='', description='List of names or descriptions of any failing tests.'
- )
- error_messages: str = Field(
- default='', description='List of key error messages encountered.'
- )
-
- # Version control fields
- branch_created: str = Field(
- default='',
- description='Whether a branch has been created for this work. True, false, or unknown.',
- )
- branch_name: str = Field(
- default='', description='Name of the current working branch if known.'
- )
- commits_made: str = Field(
- default='',
- description='Whether any commits have been made. True, false, or unknown.',
- )
- pr_created: str = Field(
- default='',
- description='Whether a pull request has been created. True, false, or unknown.',
- )
- pr_status: str = Field(
- default='',
- description="Status of any pull request: 'draft', 'open', 'merged', 'closed', or 'unknown'.",
- )
-
- # Other fields
- dependencies: str = Field(
- default='',
- description='List of dependencies or imports that have been added or modified.',
- )
- other_relevant_context: str = Field(
- default='',
- description="Any other important information that doesn't fit into the categories above.",
- )
-
- @classmethod
- def tool_description(cls) -> dict[str, Any]:
- """Description of a tool whose arguments are the fields of this class.
-
- Can be given to an LLM to force structured generation.
- """
- properties = {}
-
- # Build properties dictionary from field information
- for field_name, field in cls.model_fields.items():
- description = field.description or ''
-
- properties[field_name] = {'type': 'string', 'description': description}
-
- return {
- 'type': 'function',
- 'function': {
- 'name': 'create_state_summary',
- 'description': 'Creates a comprehensive summary of the current state of the interaction to preserve context when history grows too large. You must include non-empty values for user_context, completed_tasks, and pending_tasks.',
- 'parameters': {
- 'type': 'object',
- 'properties': properties,
- 'required': ['user_context', 'completed_tasks', 'pending_tasks'],
- },
- },
- }
-
- def __str__(self) -> str:
- """Format the state summary in a clear way for Claude 3.7 Sonnet."""
- sections = [
- '# State Summary',
- '## Core Information',
- f'**User Context**: {self.user_context}',
- f'**Completed Tasks**: {self.completed_tasks}',
- f'**Pending Tasks**: {self.pending_tasks}',
- f'**Current State**: {self.current_state}',
- '## Code Changes',
- f'**Files Modified**: {self.files_modified}',
- f'**Function Changes**: {self.function_changes}',
- f'**Data Structures**: {self.data_structures}',
- f'**Dependencies**: {self.dependencies}',
- '## Testing Status',
- f'**Tests Written**: {self.tests_written}',
- f'**Tests Passing**: {self.tests_passing}',
- f'**Failing Tests**: {self.failing_tests}',
- f'**Error Messages**: {self.error_messages}',
- '## Version Control',
- f'**Branch Created**: {self.branch_created}',
- f'**Branch Name**: {self.branch_name}',
- f'**Commits Made**: {self.commits_made}',
- f'**PR Created**: {self.pr_created}',
- f'**PR Status**: {self.pr_status}',
- '## Additional Context',
- f'**Other Relevant Context**: {self.other_relevant_context}',
- ]
-
- # Join all sections with double newlines
- return '\n\n'.join(sections)
-
-
-class StructuredSummaryCondenser(RollingCondenser):
- """A condenser that summarizes forgotten events.
-
- Maintains a condensed history and forgets old events when it grows too large. Uses structured generation via function-calling to produce summaries that replace forgotten events.
- """
-
- def __init__(
- self,
- llm: LLM,
- max_size: int = 100,
- keep_first: int = 1,
- max_event_length: int = 10_000,
- ):
- if keep_first >= max_size // 2:
- raise ValueError(
- f'keep_first ({keep_first}) must be less than half of max_size ({max_size})'
- )
- if keep_first < 0:
- raise ValueError(f'keep_first ({keep_first}) cannot be negative')
- if max_size < 1:
- raise ValueError(f'max_size ({max_size}) cannot be non-positive')
-
- self.max_size = max_size
- self.keep_first = keep_first
- self.max_event_length = max_event_length
- self.llm = llm
- if not self.llm.is_function_calling_active():
- raise ValueError(
- 'LLM must support function calling to use StructuredSummaryCondenser'
- )
-
- super().__init__()
-
- def _truncate(self, content: str) -> str:
- """Truncate the content to fit within the specified maximum event length."""
- return truncate_content(content, max_chars=self.max_event_length)
-
- def get_condensation(self, view: View) -> Condensation:
- head = view[: self.keep_first]
- target_size = self.max_size // 2
- # Number of events to keep from the tail -- target size, minus however many
- # prefix events from the head, minus one for the summarization event
- events_from_tail = target_size - len(head) - 1
-
- summary_event = (
- view[self.keep_first]
- if isinstance(view[self.keep_first], AgentCondensationObservation)
- else AgentCondensationObservation('No events summarized')
- )
-
- # Identify events to be forgotten (those not in head or tail)
- forgotten_events = []
- for event in view[self.keep_first : -events_from_tail]:
- if not isinstance(event, AgentCondensationObservation):
- forgotten_events.append(event)
-
- # Construct prompt for summarization
- prompt = """You are maintaining a context-aware state summary for an interactive software agent. This summary is critical because it:
-1. Preserves essential context when conversation history grows too large
-2. Prevents lost work when the session length exceeds token limits
-3. Helps maintain continuity across multiple interactions
-
-You will be given:
-- A list of events (actions taken by the agent)
-- The most recent previous summary (if one exists)
-
-Capture all relevant information, especially:
-- User requirements that were explicitly stated
-- Work that has been completed
-- Tasks that remain pending
-- Current state of code, variables, and data structures
-- The status of any version control operations"""
-
- prompt += '\n\n'
-
- # Add the previous summary if it exists. We'll always have a summary
- # event, but the types aren't precise enought to guarantee that it has a
- # message attribute.
- summary_event_content = self._truncate(
- summary_event.message if summary_event.message else ''
- )
- prompt += f'<PREVIOUS SUMMARY>\n{summary_event_content}\n</PREVIOUS SUMMARY>\n'
-
- prompt += '\n\n'
-
- # Add all events that are being forgotten. We use the string
- # representation defined by the event, and truncate it if necessary.
- for forgotten_event in forgotten_events:
- event_content = self._truncate(str(forgotten_event))
- prompt += f'<EVENT id={forgotten_event.id}>\n{event_content}\n</EVENT>\n'
-
- messages = [Message(role='user', content=[TextContent(text=prompt)])]
-
- response = self.llm.completion(
- messages=self.llm.format_messages_for_llm(messages),
- tools=[StateSummary.tool_description()],
- tool_choice={
- 'type': 'function',
- 'function': {'name': 'create_state_summary'},
- },
- )
-
- try:
- # Extract the message containing tool calls
- message = response.choices[0].message
-
- # Check if there are tool calls
- if not hasattr(message, 'tool_calls') or not message.tool_calls:
- raise ValueError('No tool calls found in response')
-
- # Find the create_state_summary tool call
- summary_tool_call = None
- for tool_call in message.tool_calls:
- if tool_call.function.name == 'create_state_summary':
- summary_tool_call = tool_call
- break
-
- if not summary_tool_call:
- raise ValueError('create_state_summary tool call not found')
-
- # Parse the arguments
- args_json = summary_tool_call.function.arguments
- args_dict = json.loads(args_json)
-
- # Create a StateSummary object
- summary = StateSummary.model_validate(args_dict)
-
- except (ValueError, AttributeError, KeyError, json.JSONDecodeError) as e:
- logger.warning(
- f'Failed to parse summary tool call: {e}. Using empty summary.'
- )
- summary = StateSummary()
-
- self.add_metadata('response', response.model_dump())
- self.add_metadata('metrics', self.llm.metrics.get())
-
- return Condensation(
- action=CondensationAction(
- forgotten_events_start_id=min(event.id for event in forgotten_events),
- forgotten_events_end_id=max(event.id for event in forgotten_events),
- summary=str(summary),
- summary_offset=self.keep_first,
- )
- )
-
- def should_condense(self, view: View) -> bool:
- return len(view) > self.max_size or view.unhandled_condensation_request
-
- @classmethod
- def from_config(
- cls, config: StructuredSummaryCondenserConfig, llm_registry: LLMRegistry
- ) -> StructuredSummaryCondenser:
- # This condenser cannot take advantage of prompt caching. If it happens
- # to be set, we'll pay for the cache writes but never get a chance to
- # save on a read.
- llm_config = config.llm_config.model_copy()
- llm_config.caching_prompt = False
- llm = llm_registry.get_llm('condenser', llm_config)
-
- return StructuredSummaryCondenser(
- llm=llm,
- max_size=config.max_size,
- keep_first=config.keep_first,
- max_event_length=config.max_event_length,
- )
-
-
-StructuredSummaryCondenser.register_config(StructuredSummaryCondenserConfig)
diff --git a/openhands/memory/memory.py b/openhands/memory/memory.py
deleted file mode 100644
index 91da394d08..0000000000
--- a/openhands/memory/memory.py
+++ /dev/null
@@ -1,405 +0,0 @@
-import asyncio
-import os
-import uuid
-from datetime import datetime, timezone
-from pathlib import Path
-from typing import Callable
-
-import openhands
-from openhands.core.config.mcp_config import MCPConfig
-from openhands.core.logger import openhands_logger as logger
-from openhands.events.action.agent import RecallAction
-from openhands.events.event import Event, EventSource
-from openhands.events.observation.agent import (
- MicroagentKnowledge,
- RecallObservation,
-)
-from openhands.events.observation.empty import NullObservation
-from openhands.events.recall_type import RecallType
-from openhands.events.stream import EventStream, EventStreamSubscriber
-from openhands.microagent import (
- BaseMicroagent,
- KnowledgeMicroagent,
- RepoMicroagent,
- load_microagents_from_dir,
-)
-from openhands.runtime.base import Runtime
-from openhands.runtime.runtime_status import RuntimeStatus
-from openhands.utils.prompt import (
- ConversationInstructions,
- RepositoryInfo,
- RuntimeInfo,
-)
-
-GLOBAL_MICROAGENTS_DIR = os.path.join(
- os.path.dirname(os.path.dirname(openhands.__file__)),
- 'skills',
-)
-
-USER_MICROAGENTS_DIR = Path.home() / '.openhands' / 'microagents'
-
-
-class Memory:
- """Memory is a component that listens to the EventStream for information retrieval actions
- (a RecallAction) and publishes observations with the content (such as RecallObservation).
- """
-
- sid: str
- event_stream: EventStream
- status_callback: Callable | None
- loop: asyncio.AbstractEventLoop | None
- repo_microagents: dict[str, RepoMicroagent]
- knowledge_microagents: dict[str, KnowledgeMicroagent]
-
- def __init__(
- self,
- event_stream: EventStream,
- sid: str,
- status_callback: Callable | None = None,
- ):
- self.event_stream = event_stream
- self.sid = sid if sid else str(uuid.uuid4())
- self.status_callback = status_callback
- self.loop = None
-
- self.event_stream.subscribe(
- EventStreamSubscriber.MEMORY,
- self.on_event,
- self.sid,
- )
-
- # Additional placeholders to store user workspace microagents
- self.repo_microagents = {}
- self.knowledge_microagents = {}
-
- # Store repository / runtime info to send them to the templating later
- self.repository_info: RepositoryInfo | None = None
- self.runtime_info: RuntimeInfo | None = None
- self.conversation_instructions: ConversationInstructions | None = None
-
- # Load global microagents (Knowledge + Repo)
- # from typically OpenHands/skills (i.e., the PUBLIC microagents)
- self._load_global_microagents()
-
- # Load user microagents from ~/.openhands/microagents/
- self._load_user_microagents()
-
- def on_event(self, event: Event):
- """Handle an event from the event stream."""
- asyncio.get_event_loop().run_until_complete(self._on_event(event))
-
- async def _on_event(self, event: Event):
- """Handle an event from the event stream asynchronously."""
- try:
- if isinstance(event, RecallAction):
- # if this is a workspace context recall (on first user message)
- # create and add a RecallObservation
- # with info about repo, runtime, instructions, etc. including microagent knowledge if any
- if (
- event.source == EventSource.USER
- and event.recall_type == RecallType.WORKSPACE_CONTEXT
- ):
- logger.debug('Workspace context recall')
- workspace_obs: RecallObservation | NullObservation | None = None
-
- workspace_obs = self._on_workspace_context_recall(event)
- if workspace_obs is None:
- workspace_obs = NullObservation(content='')
-
- # important: this will release the execution flow from waiting for the retrieval to complete
- workspace_obs._cause = event.id # type: ignore[union-attr]
-
- self.event_stream.add_event(workspace_obs, EventSource.ENVIRONMENT)
- return
-
- # Handle knowledge recall (triggered microagents)
- # Allow triggering from both user and agent messages
- elif (
- event.source == EventSource.USER
- or event.source == EventSource.AGENT
- ) and event.recall_type == RecallType.KNOWLEDGE:
- logger.debug(
- f'Microagent knowledge recall from {event.source} message'
- )
- microagent_obs: RecallObservation | NullObservation | None = None
- microagent_obs = self._on_microagent_recall(event)
- if microagent_obs is None:
- microagent_obs = NullObservation(content='')
-
- # important: this will release the execution flow from waiting for the retrieval to complete
- microagent_obs._cause = event.id # type: ignore[union-attr]
-
- self.event_stream.add_event(microagent_obs, EventSource.ENVIRONMENT)
- return
- except Exception as e:
- error_str = f'Error: {str(e.__class__.__name__)}'
- logger.error(error_str)
- self.set_runtime_status(RuntimeStatus.ERROR_MEMORY, error_str)
- return
-
- def _on_workspace_context_recall(
- self, event: RecallAction
- ) -> RecallObservation | None:
- """Add repository and runtime information to the stream as a RecallObservation.
-
- This method collects information from all available repo microagents and concatenates their contents.
- Multiple repo microagents are supported, and their contents will be concatenated with newlines between them.
- """
- # Create WORKSPACE_CONTEXT info:
- # - repository_info
- # - runtime_info
- # - repository_instructions
- # - microagent_knowledge
-
- # Collect raw repository instructions
- repo_instructions = ''
-
- # Retrieve the context of repo instructions from all repo microagents
- for microagent in self.repo_microagents.values():
- if repo_instructions:
- repo_instructions += '\n\n'
- repo_instructions += microagent.content
-
- # Find any matched microagents based on the query
- microagent_knowledge = self._find_microagent_knowledge(event.query)
-
- # Create observation if we have anything
- if (
- self.repository_info
- or self.runtime_info
- or repo_instructions
- or microagent_knowledge
- or self.conversation_instructions
- ):
- obs = RecallObservation(
- recall_type=RecallType.WORKSPACE_CONTEXT,
- repo_name=(
- self.repository_info.repo_name
- if self.repository_info
- and self.repository_info.repo_name is not None
- else ''
- ),
- repo_directory=(
- self.repository_info.repo_directory
- if self.repository_info
- and self.repository_info.repo_directory is not None
- else ''
- ),
- repo_branch=(
- self.repository_info.branch_name
- if self.repository_info
- and self.repository_info.branch_name is not None
- else ''
- ),
- repo_instructions=repo_instructions if repo_instructions else '',
- runtime_hosts=(
- self.runtime_info.available_hosts
- if self.runtime_info
- and self.runtime_info.available_hosts is not None
- else {}
- ),
- additional_agent_instructions=(
- self.runtime_info.additional_agent_instructions
- if self.runtime_info
- and self.runtime_info.additional_agent_instructions is not None
- else ''
- ),
- microagent_knowledge=microagent_knowledge,
- content='Added workspace context',
- date=self.runtime_info.date if self.runtime_info is not None else '',
- custom_secrets_descriptions=(
- self.runtime_info.custom_secrets_descriptions
- if self.runtime_info is not None
- else {}
- ),
- conversation_instructions=(
- self.conversation_instructions.content
- if self.conversation_instructions is not None
- else ''
- ),
- working_dir=self.runtime_info.working_dir if self.runtime_info else '',
- )
- return obs
- return None
-
- def _on_microagent_recall(
- self,
- event: RecallAction,
- ) -> RecallObservation | None:
- """When a microagent action triggers microagents, create a RecallObservation with structured data."""
- # Find any matched microagents based on the query
- microagent_knowledge = self._find_microagent_knowledge(event.query)
-
- # Create observation if we have anything
- if microagent_knowledge:
- obs = RecallObservation(
- recall_type=RecallType.KNOWLEDGE,
- microagent_knowledge=microagent_knowledge,
- content='Retrieved knowledge from microagents',
- )
- return obs
- return None
-
- def _find_microagent_knowledge(self, query: str) -> list[MicroagentKnowledge]:
- """Find microagent knowledge based on a query.
-
- Args:
- query: The query to search for microagent triggers
-
- Returns:
- A list of MicroagentKnowledge objects for matched triggers
- """
- recalled_content: list[MicroagentKnowledge] = []
-
- # skip empty queries
- if not query:
- return recalled_content
-
- # Search for microagent triggers in the query
- for name, microagent in self.knowledge_microagents.items():
- trigger = microagent.match_trigger(query)
- if trigger:
- logger.info("Microagent '%s' triggered by keyword '%s'", name, trigger)
- recalled_content.append(
- MicroagentKnowledge(
- name=microagent.name,
- trigger=trigger,
- content=microagent.content,
- )
- )
- return recalled_content
-
- def load_user_workspace_microagents(
- self, user_microagents: list[BaseMicroagent]
- ) -> None:
- """This method loads microagents from a user's cloned repo or workspace directory.
-
- This is typically called from agent_session or setup once the workspace is cloned.
- """
- logger.info(
- 'Loading user workspace microagents: %s', [m.name for m in user_microagents]
- )
- for user_microagent in user_microagents:
- if isinstance(user_microagent, KnowledgeMicroagent):
- self.knowledge_microagents[user_microagent.name] = user_microagent
- elif isinstance(user_microagent, RepoMicroagent):
- self.repo_microagents[user_microagent.name] = user_microagent
-
- def _load_global_microagents(self) -> None:
- """Loads microagents from the global microagents_dir"""
- repo_agents, knowledge_agents = load_microagents_from_dir(
- GLOBAL_MICROAGENTS_DIR
- )
- for name, agent_knowledge in knowledge_agents.items():
- self.knowledge_microagents[name] = agent_knowledge
- for name, agent_repo in repo_agents.items():
- self.repo_microagents[name] = agent_repo
-
- def _load_user_microagents(self) -> None:
- """Loads microagents from the user's home directory (~/.openhands/microagents/)
- Creates the directory if it doesn't exist.
- """
- try:
- # Create the user microagents directory if it doesn't exist
- os.makedirs(USER_MICROAGENTS_DIR, exist_ok=True)
-
- # Load microagents from user directory
- repo_agents, knowledge_agents = load_microagents_from_dir(
- USER_MICROAGENTS_DIR
- )
-
- for name, agent_knowledge in knowledge_agents.items():
- self.knowledge_microagents[name] = agent_knowledge
- for name, agent_repo in repo_agents.items():
- self.repo_microagents[name] = agent_repo
- except Exception as e:
- logger.warning(
- f'Failed to load user microagents from {USER_MICROAGENTS_DIR}: {str(e)}'
- )
-
- def get_microagent_mcp_tools(self) -> list[MCPConfig]:
- """Get MCP tools from all repo microagents (always active)
-
- Returns:
- A list of MCP tools configurations from microagents
- """
- mcp_configs: list[MCPConfig] = []
-
- # Check all repo microagents for MCP tools (always active)
- for agent in self.repo_microagents.values():
- if agent.metadata.mcp_tools:
- mcp_configs.append(agent.metadata.mcp_tools)
- logger.debug(
- f'Found MCP tools in repo microagent {agent.name}: {agent.metadata.mcp_tools}'
- )
-
- return mcp_configs
-
- def set_repository_info(
- self, repo_name: str, repo_directory: str, branch_name: str | None = None
- ) -> None:
- """Store repository info so we can reference it in an observation."""
- if repo_name or repo_directory:
- self.repository_info = RepositoryInfo(
- repo_name, repo_directory, branch_name
- )
- else:
- self.repository_info = None
-
- def set_runtime_info(
- self,
- runtime: Runtime,
- custom_secrets_descriptions: dict[str, str],
- working_dir: str,
- ) -> None:
- """Store runtime info (web hosts, ports, etc.)."""
- # e.g. { '127.0.0.1': 8080 }
- utc_now = datetime.now(timezone.utc)
- date = str(utc_now.date())
-
- if runtime.web_hosts or runtime.additional_agent_instructions:
- self.runtime_info = RuntimeInfo(
- available_hosts=runtime.web_hosts,
- additional_agent_instructions=runtime.additional_agent_instructions,
- date=date,
- custom_secrets_descriptions=custom_secrets_descriptions,
- working_dir=working_dir,
- )
- else:
- self.runtime_info = RuntimeInfo(
- date=date,
- custom_secrets_descriptions=custom_secrets_descriptions,
- working_dir=working_dir,
- )
-
- def set_conversation_instructions(
- self, conversation_instructions: str | None
- ) -> None:
- """Set contextual information for conversation
- This is information the agent may require
- """
- self.conversation_instructions = ConversationInstructions(
- content=conversation_instructions or ''
- )
-
- def set_runtime_status(self, status: RuntimeStatus, message: str):
- """Sends an error message if the callback function was provided."""
- if self.status_callback:
- try:
- if self.loop is None:
- self.loop = asyncio.get_running_loop()
- asyncio.run_coroutine_threadsafe(
- self._set_runtime_status('error', status, message), self.loop
- )
- except (RuntimeError, KeyError) as e:
- logger.error(
- f'Error sending status message: {e.__class__.__name__}',
- stack_info=False,
- )
-
- async def _set_runtime_status(
- self, msg_type: str, runtime_status: RuntimeStatus, message: str
- ):
- """Sends a status message to the client."""
- if self.status_callback:
- self.status_callback(msg_type, runtime_status, message)
diff --git a/tests/unit/controller/test_agent_delegation.py b/tests/unit/controller/test_agent_delegation.py
index dab792c708..013b298611 100644
--- a/tests/unit/controller/test_agent_delegation.py
+++ b/tests/unit/controller/test_agent_delegation.py
@@ -7,10 +7,6 @@ import pytest
from openhands.controller.agent import Agent
from openhands.controller.agent_controller import AgentController
-from openhands.controller.state.control_flags import (
- BudgetControlFlag,
- IterationControlFlag,
-)
from openhands.controller.state.state import State
from openhands.core.config import OpenHandsConfig
from openhands.core.config.agent_config import AgentConfig
@@ -18,21 +14,12 @@ from openhands.core.config.llm_config import LLMConfig
from openhands.core.schema import AgentState
from openhands.events import EventSource, EventStream
from openhands.events.action import (
- AgentDelegateAction,
- AgentFinishAction,
MessageAction,
)
-from openhands.events.action.agent import RecallAction
-from openhands.events.action.commands import CmdRunAction
from openhands.events.action.message import SystemMessageAction
-from openhands.events.event import Event
-from openhands.events.observation.agent import RecallObservation
-from openhands.events.recall_type import RecallType
-from openhands.events.stream import EventStreamSubscriber
from openhands.llm.llm import LLM
from openhands.llm.llm_registry import LLMRegistry
from openhands.llm.metrics import Metrics
-from openhands.memory.memory import Memory
from openhands.server.services.conversation_stats import ConversationStats
from openhands.storage.memory import InMemoryFileStore
@@ -127,163 +114,6 @@ def create_mock_agent_factory(mock_child_agent, llm_registry):
return create_mock_agent
-async def test_delegation_flow(
- mock_parent_agent, mock_child_agent, mock_event_stream, connected_registry_and_stats
-):
- """
- Test that when the parent agent delegates to a child
- 1. the parent's delegate is set, and once the child finishes, the parent is cleaned up properly.
- 2. metrics are accumulated globally via LLM registry (delegate adds to the global metrics)
- 3. global metrics tracking works correctly through the LLM registry
- """
- llm_registry, conversation_stats = connected_registry_and_stats
-
- # Mock the agent class resolution so that AgentController can instantiate mock_child_agent
- Agent.get_cls = Mock(
- return_value=create_mock_agent_factory(mock_child_agent, llm_registry)
- )
-
- step_count = 0
-
- def agent_step_fn(state):
- nonlocal step_count
- step_count += 1
- return CmdRunAction(command=f'ls {step_count}')
-
- mock_child_agent.step = agent_step_fn
-
- # Set up the parent agent's LLM with initial cost and register it in the registry
- # The parent agent's LLM should use the existing registered LLM to ensure proper tracking
- parent_llm = llm_registry.service_to_llm['agent']
- parent_llm.metrics.accumulated_cost = 2
- mock_parent_agent.llm = parent_llm
-
- parent_metrics = Metrics()
- parent_metrics.accumulated_cost = 2
- # Create parent controller
- parent_state = State(
- inputs={},
- metrics=parent_metrics,
- budget_flag=BudgetControlFlag(
- current_value=2, limit_increase_amount=10, max_value=10
- ),
- iteration_flag=IterationControlFlag(
- current_value=1, limit_increase_amount=10, max_value=10
- ),
- )
-
- parent_controller = AgentController(
- agent=mock_parent_agent,
- event_stream=mock_event_stream,
- conversation_stats=conversation_stats,
- iteration_delta=1, # Add the required iteration_delta parameter
- sid='parent',
- confirmation_mode=False,
- headless_mode=True,
- initial_state=parent_state,
- )
-
- # Setup Memory to catch RecallActions
- mock_memory = MagicMock(spec=Memory)
- mock_memory.event_stream = mock_event_stream
-
- def on_event(event: Event):
- if isinstance(event, RecallAction):
- # create a RecallObservation
- microagent_observation = RecallObservation(
- recall_type=RecallType.KNOWLEDGE,
- content='Found info',
- )
- microagent_observation._cause = event.id # ignore attr-defined warning
- mock_event_stream.add_event(microagent_observation, EventSource.ENVIRONMENT)
-
- mock_memory.on_event = on_event
- mock_event_stream.subscribe(
- EventStreamSubscriber.MEMORY, mock_memory.on_event, mock_memory
- )
-
- # Setup a delegate action from the parent
- delegate_action = AgentDelegateAction(agent='ChildAgent', inputs={'test': True})
- mock_parent_agent.step.return_value = delegate_action
-
- # Simulate a user message event to cause parent.step() to run
- message_action = MessageAction(content='please delegate now')
- message_action._source = EventSource.USER
- await parent_controller._on_event(message_action)
-
- # Give time for the async step() to execute
- await asyncio.sleep(1)
-
- # Verify that a RecallObservation was added to the event stream
- events = list(mock_event_stream.get_events())
-
- # The exact number of events might vary depending on implementation details
- # Just verify that we have at least a few events
- assert mock_event_stream.get_latest_event_id() >= 3
-
- # a RecallObservation and an AgentDelegateAction should be in the list
- assert any(isinstance(event, RecallObservation) for event in events)
- assert any(isinstance(event, AgentDelegateAction) for event in events)
-
- # Verify that a delegate agent controller is created
- assert parent_controller.delegate is not None, (
- "Parent's delegate controller was not set."
- )
-
- # The parent's iteration should have incremented
- assert parent_controller.state.iteration_flag.current_value == 2, (
- 'Parent iteration should be incremented after step.'
- )
-
- # Now simulate that the child increments local iteration and finishes its subtask
- delegate_controller = parent_controller.delegate
-
- # Take four delegate steps; mock cost per step
- for i in range(4):
- delegate_controller.state.iteration_flag.step()
- delegate_controller.agent.step(delegate_controller.state)
- # Update the agent's LLM metrics (not the deprecated state metrics)
- delegate_controller.agent.llm.metrics.add_cost(1.0)
-
- assert (
- delegate_controller.state.get_local_step() == 4
- ) # verify local metrics are accessible via snapshot
-
- # Check that the conversation stats has the combined metrics (parent + delegate)
- combined_metrics = (
- delegate_controller.state.conversation_stats.get_combined_metrics()
- )
- assert (
- combined_metrics.accumulated_cost
- == 6 # Make sure delegate tracks global cost (2 from parent + 4 from delegate)
- )
-
- # Since metrics are now global via LLM registry, local metrics tracking
- # is handled differently. The delegate's LLM shares the same metrics object
- # as the parent for global tracking, so we verify the global total is correct.
-
- delegate_controller.state.outputs = {'delegate_result': 'done'}
-
- # The child is done, so we simulate it finishing:
- child_finish_action = AgentFinishAction()
- await delegate_controller._on_event(child_finish_action)
- await asyncio.sleep(0.5)
-
- # Now the parent's delegate is None
- assert parent_controller.delegate is None, (
- 'Parent delegate should be None after child finishes.'
- )
-
- # Parent's global iteration is updated from the child
- assert parent_controller.state.iteration_flag.current_value == 7, (
- "Parent iteration should be the child's iteration + 1 after child is done."
- )
-
- # Cleanup
- await parent_controller.close()
-
-
@pytest.mark.asyncio
@pytest.mark.parametrize(
'delegate_state',
@@ -358,132 +188,3 @@ async def test_delegate_step_different_states(
assert mock_delegate.close.call_count == 1
await controller.close()
-
-
-async def test_delegate_hits_global_limits(
- mock_child_agent, mock_event_stream, mock_parent_agent, connected_registry_and_stats
-):
- """
- Global limits from control flags should apply to delegates
- """
- llm_registry, conversation_stats = connected_registry_and_stats
-
- # Mock the agent class resolution so that AgentController can instantiate mock_child_agent
- Agent.get_cls = Mock(
- return_value=create_mock_agent_factory(mock_child_agent, llm_registry)
- )
-
- # Set up the parent agent's LLM with initial cost and register it in the registry
- mock_parent_agent.llm.metrics.accumulated_cost = 2
- mock_parent_agent.llm.service_id = 'main_agent'
- # Register the parent agent's LLM in the registry
- llm_registry.service_to_llm['main_agent'] = mock_parent_agent.llm
-
- parent_metrics = Metrics()
- parent_metrics.accumulated_cost = 2
- # Create parent controller
- parent_state = State(
- inputs={},
- metrics=parent_metrics,
- budget_flag=BudgetControlFlag(
- current_value=2, limit_increase_amount=10, max_value=10
- ),
- iteration_flag=IterationControlFlag(
- current_value=2, limit_increase_amount=3, max_value=3
- ),
- )
-
- parent_controller = AgentController(
- agent=mock_parent_agent,
- event_stream=mock_event_stream,
- conversation_stats=conversation_stats,
- iteration_delta=1, # Add the required iteration_delta parameter
- sid='parent',
- confirmation_mode=False,
- headless_mode=False,
- initial_state=parent_state,
- )
-
- # Setup Memory to catch RecallActions
- mock_memory = MagicMock(spec=Memory)
- mock_memory.event_stream = mock_event_stream
-
- def on_event(event: Event):
- if isinstance(event, RecallAction):
- # create a RecallObservation
- microagent_observation = RecallObservation(
- recall_type=RecallType.KNOWLEDGE,
- content='Found info',
- )
- microagent_observation._cause = event.id # ignore attr-defined warning
- mock_event_stream.add_event(microagent_observation, EventSource.ENVIRONMENT)
-
- mock_memory.on_event = on_event
- mock_event_stream.subscribe(
- EventStreamSubscriber.MEMORY, mock_memory.on_event, mock_memory
- )
-
- # Setup a delegate action from the parent
- delegate_action = AgentDelegateAction(agent='ChildAgent', inputs={'test': True})
- mock_parent_agent.step.return_value = delegate_action
-
- # Simulate a user message event to cause parent.step() to run
- message_action = MessageAction(content='please delegate now')
- message_action._source = EventSource.USER
- await parent_controller._on_event(message_action)
-
- # Give time for the async step() to execute
- await asyncio.sleep(1)
-
- # Verify that a RecallObservation was added to the event stream
- events = list(mock_event_stream.get_events())
-
- # The exact number of events might vary depending on implementation details
- # Just verify that we have at least a few events
- assert mock_event_stream.get_latest_event_id() >= 3
-
- # a RecallObservation and an AgentDelegateAction should be in the list
- assert any(isinstance(event, RecallObservation) for event in events)
- assert any(isinstance(event, AgentDelegateAction) for event in events)
-
- # Verify that a delegate agent controller is created
- assert parent_controller.delegate is not None, (
- "Parent's delegate controller was not set."
- )
-
- delegate_controller = parent_controller.delegate
- await delegate_controller.set_agent_state_to(AgentState.RUNNING)
-
- # Step should hit max budget
- message_action = MessageAction(content='Test message')
- message_action._source = EventSource.USER
-
- await delegate_controller._on_event(message_action)
- await asyncio.sleep(0.1)
-
- assert delegate_controller.state.agent_state == AgentState.ERROR
- assert (
- delegate_controller.state.last_error
- == 'RuntimeError: Agent reached maximum iteration. Current iteration: 3, max iteration: 3'
- )
-
- await delegate_controller.set_agent_state_to(AgentState.RUNNING)
- await asyncio.sleep(0.1)
-
- assert delegate_controller.state.iteration_flag.max_value == 6
- assert (
- delegate_controller.state.iteration_flag.max_value
- == parent_controller.state.iteration_flag.max_value
- )
-
- message_action = MessageAction(content='Test message 2')
- message_action._source = EventSource.USER
- await delegate_controller._on_event(message_action)
- await asyncio.sleep(0.1)
-
- assert delegate_controller.state.iteration_flag.current_value == 4
- assert (
- delegate_controller.state.iteration_flag.current_value
- == parent_controller.state.iteration_flag.current_value
- )
diff --git a/tests/unit/memory/condenser/test_condenser.py b/tests/unit/memory/condenser/test_condenser.py
deleted file mode 100644
index 4e6365c8ed..0000000000
--- a/tests/unit/memory/condenser/test_condenser.py
+++ /dev/null
@@ -1,848 +0,0 @@
-from datetime import datetime
-from typing import Any, Callable, Iterable
-from unittest.mock import MagicMock
-
-import pytest
-
-from openhands.controller.state.state import State
-from openhands.core.config.condenser_config import (
- AmortizedForgettingCondenserConfig,
- BrowserOutputCondenserConfig,
- CondenserPipelineConfig,
- LLMAttentionCondenserConfig,
- LLMSummarizingCondenserConfig,
- NoOpCondenserConfig,
- ObservationMaskingCondenserConfig,
- RecentEventsCondenserConfig,
- StructuredSummaryCondenserConfig,
-)
-from openhands.core.config.llm_config import LLMConfig
-from openhands.core.config.openhands_config import OpenHandsConfig
-from openhands.core.message import Message, TextContent
-from openhands.core.schema.action import ActionType
-from openhands.events.event import Event, EventSource
-from openhands.events.observation import BrowserOutputObservation
-from openhands.events.observation.agent import AgentCondensationObservation
-from openhands.events.observation.observation import Observation
-from openhands.llm import LLM
-from openhands.llm.llm_registry import LLMRegistry
-from openhands.memory.condenser import Condenser
-from openhands.memory.condenser.condenser import Condensation, RollingCondenser, View
-from openhands.memory.condenser.impl import (
- AmortizedForgettingCondenser,
- BrowserOutputCondenser,
- ImportantEventSelection,
- LLMAttentionCondenser,
- LLMSummarizingCondenser,
- NoOpCondenser,
- ObservationMaskingCondenser,
- RecentEventsCondenser,
- StructuredSummaryCondenser,
-)
-from openhands.memory.condenser.impl.pipeline import CondenserPipeline
-from openhands.server.services.conversation_stats import ConversationStats
-
-
-def create_test_event(
- message: str, timestamp: datetime | None = None, id: int | None = None
-) -> Event:
- """Create a simple test event."""
- event = Event()
- event._message = message
- event.timestamp = timestamp if timestamp else datetime.now()
- if id is not None:
- event._id = id
- event._source = EventSource.USER
- return event
-
-
-def mock_llm() -> LLM:
- """Mocks an LLM object with a utility function for setting and resetting response contents in unit tests."""
- # Create a real LLMConfig instead of a mock to properly handle SecretStr api_key
- real_config = LLMConfig(
- model='gpt-4o', api_key='test_key', custom_llm_provider=None
- )
-
- # Create a MagicMock for the LLM object
- mock_llm = MagicMock(
- spec=LLM,
- config=real_config,
- metrics=MagicMock(),
- )
- _mock_content = None
-
- # Set a mock message with the mocked content
- mock_message = MagicMock()
- mock_message.content = _mock_content
-
- def set_mock_response_content(content: Any):
- """Set the mock response for the LLM."""
- nonlocal mock_message
- mock_message.content = content
-
- mock_choice = MagicMock()
- mock_choice.message = mock_message
-
- mock_response = MagicMock()
- mock_response.choices = [mock_choice]
-
- mock_llm.completion.return_value = mock_response
-
- # Attach helper methods to the mock object
- mock_llm.set_mock_response_content = set_mock_response_content
-
- mock_llm.format_messages_for_llm = lambda events: [
- Message(role='user', content=[TextContent(text=str(event))]) for event in events
- ]
-
- mock_llm.is_function_calling_active.return_value = True
-
- return mock_llm
-
-
-def mock_conversation_stats() -> ConversationStats:
- """Creates a mock ConversationStats service."""
- mock_stats = MagicMock(spec=ConversationStats)
- return mock_stats
-
-
-def mock_llm_registry(mock_llm, mock_conversation_stats) -> LLMRegistry:
- """Creates an actual LLMRegistry that returns real LLMs."""
- # Create an actual LLMRegistry with a basic OpenHandsConfig
- config = OpenHandsConfig()
- registry = LLMRegistry(config=config, agent_cls=None, retry_listener=None)
-
- return registry
-
-
-class RollingCondenserTestHarness:
- """Test harness for rolling condensers.
-
- Simulates the behavior of a simple agent loop (appropriately handling the distinction between `View` and `Condensation` results) and provides utilities for testing the results.
- """
-
- def __init__(self, condenser: RollingCondenser):
- self.condenser = condenser
- self.callbacks: list[Callable[[list[Event]], None]] = []
-
- def add_callback(self, callback: Callable[[list[Event]], None]):
- """Add a callback to the test harness.
-
- This callback will be called on the history after each event is added, but before the condenser is applied. You can use this to export information about the event that was just added, or to set LLM responses based on the state.
- """
- self.callbacks.append(callback)
-
- def views(self, events: Iterable[Event]) -> Iterable[View]:
- """Generate a sequence of views similating the condenser's behavior over the given event stream.
-
- This generator assumes we're starting from an empty history.
- """
- state = State()
-
- for event in events:
- # Set the event's ID -- this is normally done by the event stream,
- # but this harness is intended to act as a testing stand-in.
- if not hasattr(event, '_id'):
- event._id = len(state.history)
-
- state.history.append(event)
- for callback in self.callbacks:
- callback(state.history)
-
- match self.condenser.condensed_history(state):
- case View() as view:
- yield view
-
- case Condensation(event=condensation_event):
- state.history.append(condensation_event)
-
- def expected_size(self, index: int, max_size: int) -> int:
- """Calculate the expected size of the view at the given index.
-
- Assumes the condenser triggers condensation when the view is _longer_ than the max size, and that the target size is half the max size.
- """
- # Until we hit the max size, the views should grow monotonically.
- if index < max_size:
- return index + 1
-
- # Once we hit the max size, the next view should be reduced to the target size.
- target_size = max_size // 2
-
- # So when the index is the same as the max size, we should have target size + 1 events in the view.
- # And the maximum value we will ever see is the max size (approximately 2 * target size).
- # Put together, we get the following formula:
- return ((index - max_size) % target_size) + target_size + 1
-
- def expected_condensations(self, index: int, max_size: int) -> int:
- """Calculate the expected number of condensation events at the given index.
-
- Assumes the condenser triggers condensation when the view is _longer_ than the max size, and that the target size is half the max size.
- """
- if index < max_size:
- return 0
-
- target_size = max_size // 2
- return ((index - max_size) // target_size) + 1
-
-
-def test_noop_condenser_from_config(mock_llm_registry):
- """Test that the NoOpCondenser objects can be made from config."""
- config = NoOpCondenserConfig()
- condenser = Condenser.from_config(config, mock_llm_registry)
-
- assert isinstance(condenser, NoOpCondenser)
-
-
-def test_noop_condenser():
- """Test that NoOpCondensers preserve their input events."""
- events = [
- create_test_event('Event 1'),
- create_test_event('Event 2'),
- create_test_event('Event 3'),
- ]
- state = State()
- state.history = events
-
- condenser = NoOpCondenser()
- result = condenser.condensed_history(state)
-
- assert result == View(events=events)
-
-
-def test_observation_masking_condenser_from_config(mock_llm_registry):
- """Test that ObservationMaskingCondenser objects can be made from config."""
- attention_window = 5
- config = ObservationMaskingCondenserConfig(attention_window=attention_window)
- condenser = Condenser.from_config(config, mock_llm_registry)
-
- assert isinstance(condenser, ObservationMaskingCondenser)
- assert condenser.attention_window == attention_window
-
-
-def test_observation_masking_condenser_respects_attention_window():
- """Test that ObservationMaskingCondenser only masks events outside the attention window."""
- attention_window = 3
- condenser = ObservationMaskingCondenser(attention_window=attention_window)
-
- events = [
- create_test_event('Event 1'),
- Observation('Observation 1'),
- create_test_event('Event 3'),
- create_test_event('Event 4'),
- Observation('Observation 2'),
- ]
-
- state = State()
- state.history = events
- result = condenser.condensed_history(state)
-
- assert len(result) == len(events)
-
- for index, (event, condensed_event) in enumerate(zip(events, result)):
- # If we're outside the attention window, observations should be masked.
- if index < len(events) - attention_window:
- if isinstance(event, Observation):
- assert '<MASKED>' in str(condensed_event)
-
- # If we're within the attention window, events are unchanged.
- else:
- assert event == condensed_event
-
-
-def test_browser_output_condenser_from_config(mock_llm_registry):
- """Test that BrowserOutputCondenser objects can be made from config."""
- attention_window = 5
- config = BrowserOutputCondenserConfig(attention_window=attention_window)
- condenser = Condenser.from_config(config, mock_llm_registry)
-
- assert isinstance(condenser, BrowserOutputCondenser)
- assert condenser.attention_window == attention_window
-
-
-def test_browser_output_condenser_respects_attention_window():
- """Test that BrowserOutputCondenser only masks events outside the attention window."""
- attention_window = 3
- condenser = BrowserOutputCondenser(attention_window=attention_window)
-
- events = [
- BrowserOutputObservation('Observation 1', url='', trigger_by_action=''),
- BrowserOutputObservation('Observation 2', url='', trigger_by_action=''),
- create_test_event('Event 3'),
- create_test_event('Event 4'),
- BrowserOutputObservation('Observation 3', url='', trigger_by_action=''),
- BrowserOutputObservation('Observation 4', url='', trigger_by_action=''),
- ]
-
- state = State()
- state.history = events
-
- result = condenser.condensed_history(state)
-
- assert len(result) == len(events)
- cnt = 4
- for event, condensed_event in zip(events, result):
- if isinstance(event, (BrowserOutputObservation, AgentCondensationObservation)):
- if cnt > attention_window:
- assert 'Content omitted' in str(condensed_event)
- else:
- assert event == condensed_event
- cnt -= 1
- else:
- assert event == condensed_event
-
-
-def test_recent_events_condenser_from_config(mock_llm_registry):
- """Test that RecentEventsCondenser objects can be made from config."""
- max_events = 5
- keep_first = True
- config = RecentEventsCondenserConfig(keep_first=keep_first, max_events=max_events)
- condenser = Condenser.from_config(config, mock_llm_registry)
-
- assert isinstance(condenser, RecentEventsCondenser)
- assert condenser.max_events == max_events
- assert condenser.keep_first == keep_first
-
-
-def test_recent_events_condenser():
- """Test that RecentEventsCondensers keep just the most recent events."""
- events = [
- create_test_event('Event 1'),
- create_test_event('Event 2'),
- create_test_event('Event 3'),
- create_test_event('Event 4'),
- create_test_event('Event 5'),
- ]
-
- state = State()
- state.history = events
-
- # If the max_events are larger than the number of events, equivalent to a NoOpCondenser.
- condenser = RecentEventsCondenser(max_events=len(events))
- result = condenser.condensed_history(state)
-
- assert result == View(events=events)
-
- # If the max_events are smaller than the number of events, only keep the last few.
- max_events = 3
- condenser = RecentEventsCondenser(max_events=max_events)
- result = condenser.condensed_history(state)
-
- assert len(result) == max_events
- assert result[0]._message == 'Event 1' # kept from keep_first
- assert result[1]._message == 'Event 4' # kept from max_events
- assert result[2]._message == 'Event 5' # kept from max_events
-
- # If the keep_first flag is set, the first event will always be present.
- keep_first = 1
- max_events = 2
- condenser = RecentEventsCondenser(keep_first=keep_first, max_events=max_events)
- result = condenser.condensed_history(state)
-
- assert len(result) == max_events
- assert result[0]._message == 'Event 1'
- assert result[1]._message == 'Event 5'
-
- # We should be able to keep more of the initial events.
- keep_first = 2
- max_events = 3
- condenser = RecentEventsCondenser(keep_first=keep_first, max_events=max_events)
- result = condenser.condensed_history(state)
-
- assert len(result) == max_events
- assert result[0]._message == 'Event 1' # kept from keep_first
- assert result[1]._message == 'Event 2' # kept from keep_first
- assert result[2]._message == 'Event 5' # kept from max_events
-
-
-def test_llm_summarizing_condenser_from_config(mock_llm_registry):
- """Test that LLMSummarizingCondenser objects can be made from config."""
- config = LLMSummarizingCondenserConfig(
- max_size=50,
- keep_first=10,
- llm_config=LLMConfig(model='gpt-4o', api_key='test_key', caching_prompt=True),
- )
- condenser = Condenser.from_config(config, mock_llm_registry)
-
- assert isinstance(condenser, LLMSummarizingCondenser)
- assert condenser.llm.config.model == 'gpt-4o'
- assert condenser.llm.config.api_key.get_secret_value() == 'test_key'
- assert condenser.max_size == 50
- assert condenser.keep_first == 10
-
-
-def test_llm_summarizing_condenser_invalid_config(mock_llm, mock_llm_registry):
- """Test that LLMSummarizingCondenser raises error when keep_first > max_size."""
- pytest.raises(
- ValueError,
- LLMSummarizingCondenser,
- llm=mock_llm,
- max_size=4,
- keep_first=2,
- )
- pytest.raises(
- ValueError,
- LLMSummarizingCondenser,
- llm=mock_llm,
- max_size=0,
- )
- pytest.raises(
- ValueError,
- LLMSummarizingCondenser,
- llm=mock_llm,
- keep_first=-1,
- )
-
-
-def test_llm_summarizing_condenser_gives_expected_view_size(
- mock_llm, mock_llm_registry
-):
- """Test that LLMSummarizingCondenser maintains the correct view size."""
- max_size = 10
- condenser = LLMSummarizingCondenser(max_size=max_size, llm=mock_llm)
-
- events = [create_test_event(f'Event {i}', id=i) for i in range(max_size * 10)]
-
- # Set up mock LLM response
- mock_llm.set_mock_response_content('Summary of forgotten events')
-
- harness = RollingCondenserTestHarness(condenser)
-
- for i, view in enumerate(harness.views(events)):
- assert len(view) == harness.expected_size(i, max_size)
-
-
-def test_llm_summarizing_condenser_keeps_first_and_summary_events(
- mock_llm, mock_llm_registry
-):
- """Test that the LLM summarizing condenser appropriately maintains the event prefix and any summary events."""
- max_size = 10
- keep_first = 3
- condenser = LLMSummarizingCondenser(
- max_size=max_size,
- keep_first=keep_first,
- llm=mock_llm,
- )
-
- mock_llm.set_mock_response_content('Summary of forgotten events')
-
- events = [create_test_event(f'Event {i}', id=i) for i in range(max_size * 10)]
- harness = RollingCondenserTestHarness(condenser)
-
- for i, view in enumerate(harness.views(events)):
- assert len(view) == harness.expected_size(i, max_size)
-
- # Ensure that the we've called out the summarizing LLM once per condensation
- assert mock_llm.completion.call_count == harness.expected_condensations(
- i, max_size
- )
-
- # Ensure that the prefix is appropiately maintained
- assert view[:keep_first] == events[: min(keep_first, i + 1)]
-
- # If we've condensed, ensure that the summary event is present
- if i > max_size:
- assert isinstance(view[keep_first], AgentCondensationObservation)
-
-
-def test_amortized_forgetting_condenser_from_config(mock_llm_registry):
- """Test that AmortizedForgettingCondenser objects can be made from config."""
- max_size = 50
- keep_first = 10
- config = AmortizedForgettingCondenserConfig(
- max_size=max_size, keep_first=keep_first
- )
- condenser = Condenser.from_config(config, mock_llm_registry)
-
- assert isinstance(condenser, AmortizedForgettingCondenser)
- assert condenser.max_size == max_size
- assert condenser.keep_first == keep_first
-
-
-def test_amortized_forgetting_condenser_invalid_config():
- """Test that AmortizedForgettingCondenser raises error when keep_first > max_size."""
- pytest.raises(ValueError, AmortizedForgettingCondenser, max_size=4, keep_first=2)
- pytest.raises(ValueError, AmortizedForgettingCondenser, max_size=0)
- pytest.raises(ValueError, AmortizedForgettingCondenser, keep_first=-1)
-
-
-def test_amortized_forgetting_condenser_gives_expected_view_size():
- """Test that AmortizedForgettingCondenser maintains a context view of the correct size."""
- max_size = 12
- condenser = AmortizedForgettingCondenser(max_size=max_size)
-
- events = [create_test_event(f'Event {i}', id=i) for i in range(max_size * 10)]
-
- harness = RollingCondenserTestHarness(condenser)
-
- for i, view in enumerate(harness.views(events)):
- assert len(view) == harness.expected_size(i, max_size)
-
-
-def test_amortized_forgetting_condenser_keeps_first_and_last_events():
- """Test that the AmortizedForgettingCondenser keeps the prefix and suffix events, even when condensing."""
- max_size = 12
- keep_first = 4
- condenser = AmortizedForgettingCondenser(max_size=max_size, keep_first=keep_first)
-
- events = [create_test_event(f'Event {i}', id=i) for i in range(max_size * 10)]
-
- # To ensure the most recent event is always recorded, track it in a non-local variable updated
- # with a closure we'll pass to the view generator as a callback.
- most_recent_event: Event | None = None
-
- def set_most_recent_event(history: list[Event]):
- nonlocal most_recent_event
- most_recent_event = history[-1]
-
- harness = RollingCondenserTestHarness(condenser)
- harness.add_callback(set_most_recent_event)
-
- for i, view in enumerate(harness.views(events)):
- assert len(view) == harness.expected_size(i, max_size)
-
- # The last event should always be the most-recently added.
- assert view[-1] == most_recent_event
-
- # The prefix should always match the list of events, up to the keep_first limit.
- assert view[:keep_first] == events[: min(keep_first, i + 1)]
-
-
-def test_llm_attention_condenser_from_config(mock_llm_registry):
- """Test that LLMAttentionCondenser objects can be made from config."""
- config = LLMAttentionCondenserConfig(
- max_size=50,
- keep_first=10,
- llm_config=LLMConfig(
- model='gpt-4o',
- api_key='test_key',
- caching_prompt=True,
- ),
- )
- condenser = Condenser.from_config(config, mock_llm_registry)
-
- assert isinstance(condenser, LLMAttentionCondenser)
- assert condenser.llm.config.model == 'gpt-4o'
- assert condenser.max_size == 50
- assert condenser.keep_first == 10
-
- # Create a mock LLM that doesn't support function calling
- mock_llm = MagicMock()
- mock_llm.is_function_calling_active.return_value = False
-
- # Create a new registry that returns our mock LLM that doesn't support function calling
- mock_registry = MagicMock(spec=LLMRegistry)
- mock_registry.get_llm.return_value = mock_llm
-
- pytest.raises(ValueError, LLMAttentionCondenser.from_config, config, mock_registry)
-
-
-def test_llm_attention_condenser_gives_expected_view_size(mock_llm, mock_llm_registry):
- """Test that the LLMAttentionCondenser gives views of the expected size."""
- max_size = 10
- condenser = LLMAttentionCondenser(
- max_size=max_size,
- keep_first=0,
- llm=mock_llm,
- )
-
- events = [create_test_event(f'Event {i}', id=i) for i in range(max_size * 10)]
-
- def set_response_content(history: list[Event]):
- mock_llm.set_mock_response_content(
- ImportantEventSelection(
- ids=[event.id for event in history]
- ).model_dump_json()
- )
-
- harness = RollingCondenserTestHarness(condenser)
- harness.add_callback(set_response_content)
-
- for i, view in enumerate(harness.views(events)):
- assert len(view) == harness.expected_size(i, max_size)
-
-
-def test_llm_attention_condenser_handles_events_outside_history(
- mock_llm, mock_llm_registry
-):
- """Test that the LLMAttentionCondenser handles event IDs that aren't from the event history."""
- max_size = 2
- condenser = LLMAttentionCondenser(
- max_size=max_size,
- keep_first=0,
- llm=mock_llm,
- )
-
- events = [create_test_event(f'Event {i}', id=i) for i in range(max_size * 10)]
-
- def set_response_content(history: list[Event]):
- mock_llm.set_mock_response_content(
- ImportantEventSelection(
- ids=[event.id for event in history] + [-1, -2, -3, -4]
- ).model_dump_json()
- )
-
- harness = RollingCondenserTestHarness(condenser)
- harness.add_callback(set_response_content)
-
- for i, view in enumerate(harness.views(events)):
- assert len(view) == harness.expected_size(i, max_size)
-
-
-def test_llm_attention_condenser_handles_too_many_events(mock_llm, mock_llm_registry):
- """Test that the LLMAttentionCondenser handles when the response contains too many event IDs."""
- max_size = 2
- condenser = LLMAttentionCondenser(
- max_size=max_size,
- keep_first=0,
- llm=mock_llm,
- )
-
- events = [create_test_event(f'Event {i}', id=i) for i in range(max_size * 10)]
-
- def set_response_content(history: list[Event]):
- mock_llm.set_mock_response_content(
- ImportantEventSelection(
- ids=[event.id for event in history] + [event.id for event in history]
- ).model_dump_json()
- )
-
- harness = RollingCondenserTestHarness(condenser)
- harness.add_callback(set_response_content)
-
- for i, view in enumerate(harness.views(events)):
- assert len(view) == harness.expected_size(i, max_size)
-
-
-def test_llm_attention_condenser_handles_too_few_events(mock_llm, mock_llm_registry):
- """Test that the LLMAttentionCondenser handles when the response contains too few event IDs."""
- max_size = 2
- # Developer note: We must specify keep_first=0 because
- # keep_first (1) >= max_size//2 (1) is invalid.
- condenser = LLMAttentionCondenser(
- max_size=max_size,
- keep_first=0,
- llm=mock_llm,
- )
-
- events = [create_test_event(f'Event {i}', id=i) for i in range(max_size * 10)]
-
- def set_response_content(history: list[Event]):
- mock_llm.set_mock_response_content(
- ImportantEventSelection(ids=[]).model_dump_json()
- )
-
- harness = RollingCondenserTestHarness(condenser)
- harness.add_callback(set_response_content)
-
- for i, view in enumerate(harness.views(events)):
- assert len(view) == harness.expected_size(i, max_size)
-
-
-def test_llm_attention_condenser_handles_keep_first_events(mock_llm, mock_llm_registry):
- """Test that LLMAttentionCondenser works when keep_first=1 is allowed (must be less than half of max_size)."""
- max_size = 12
- keep_first = 4
- condenser = LLMAttentionCondenser(
- max_size=max_size,
- keep_first=keep_first,
- llm=mock_llm,
- )
-
- events = [create_test_event(f'Event {i}', id=i) for i in range(max_size * 10)]
-
- def set_response_content(history: list[Event]):
- mock_llm.set_mock_response_content(
- ImportantEventSelection(ids=[]).model_dump_json()
- )
-
- harness = RollingCondenserTestHarness(condenser)
- harness.add_callback(set_response_content)
-
- for i, view in enumerate(harness.views(events)):
- assert len(view) == harness.expected_size(i, max_size)
- assert view[:keep_first] == events[: min(keep_first, i + 1)]
-
-
-def test_structured_summary_condenser_from_config(mock_llm_registry):
- """Test that StructuredSummaryCondenser objects can be made from config."""
- config = StructuredSummaryCondenserConfig(
- max_size=50,
- keep_first=10,
- llm_config=LLMConfig(
- model='gpt-4o',
- api_key='test_key',
- caching_prompt=True,
- ),
- )
- condenser = Condenser.from_config(config, mock_llm_registry)
-
- assert isinstance(condenser, StructuredSummaryCondenser)
- assert condenser.llm.config.model == 'gpt-4o'
- assert condenser.llm.config.api_key.get_secret_value() == 'test_key'
- assert condenser.max_size == 50
- assert condenser.keep_first == 10
-
-
-def test_structured_summary_condenser_invalid_config(mock_llm):
- """Test that StructuredSummaryCondenser raises error when keep_first > max_size."""
- # Since the condenser only works when function calling is on, we need to
- # mock up the check for that.
- mock_llm.is_function_calling_active.return_value = True
-
- pytest.raises(
- ValueError,
- StructuredSummaryCondenser,
- llm=mock_llm,
- max_size=4,
- keep_first=2,
- )
-
- pytest.raises(
- ValueError,
- StructuredSummaryCondenser,
- llm=mock_llm,
- max_size=0,
- )
- pytest.raises(
- ValueError,
- StructuredSummaryCondenser,
- llm=mock_llm,
- keep_first=-1,
- )
-
- # If all other parameters are good but there's no function calling the
- # condenser still counts as improperly configured.
- # Create a mock LLM that doesn't support function calling
- mock_llm_no_func = MagicMock()
- mock_llm_no_func.is_function_calling_active.return_value = False
-
- pytest.raises(
- ValueError,
- StructuredSummaryCondenser,
- llm=mock_llm_no_func,
- max_size=40,
- keep_first=2,
- )
-
-
-def test_structured_summary_condenser_gives_expected_view_size(
- mock_llm, mock_llm_registry
-):
- """Test that StructuredSummaryCondenser maintains the correct view size."""
- max_size = 10
- mock_llm.is_function_calling_active.return_value = True
- condenser = StructuredSummaryCondenser(max_size=max_size, llm=mock_llm)
-
- events = [create_test_event(f'Event {i}', id=i) for i in range(max_size * 10)]
-
- # Set up mock LLM response
- mock_llm.set_mock_response_content('Summary of forgotten events')
-
- harness = RollingCondenserTestHarness(condenser)
-
- for i, view in enumerate(harness.views(events)):
- assert len(view) == harness.expected_size(i, max_size)
-
-
-def test_structured_summary_condenser_keeps_first_and_summary_events(
- mock_llm, mock_llm_registry
-):
- """Test that the StructuredSummaryCondenser appropriately maintains the event prefix and any summary events."""
- max_size = 10
- keep_first = 3
- mock_llm.is_function_calling_active.return_value = True
- condenser = StructuredSummaryCondenser(
- max_size=max_size,
- keep_first=keep_first,
- llm=mock_llm,
- )
-
- mock_llm.set_mock_response_content('Summary of forgotten events')
-
- events = [create_test_event(f'Event {i}', id=i) for i in range(max_size * 10)]
- harness = RollingCondenserTestHarness(condenser)
-
- for i, view in enumerate(harness.views(events)):
- assert len(view) == harness.expected_size(i, max_size)
-
- # Ensure that the we've called out the summarizing LLM once per condensation
- assert mock_llm.completion.call_count == harness.expected_condensations(
- i, max_size
- )
-
- # Ensure that the prefix is appropriately maintained
- assert view[:keep_first] == events[: min(keep_first, i + 1)]
-
- # If we've condensed, ensure that the summary event is present
- if i > max_size:
- assert isinstance(view[keep_first], AgentCondensationObservation)
-
-
-def test_condenser_pipeline_from_config(mock_llm_registry):
- """Test that CondenserPipeline condensers can be created from configuration objects."""
- config = CondenserPipelineConfig(
- condensers=[
- NoOpCondenserConfig(),
- BrowserOutputCondenserConfig(attention_window=2),
- LLMSummarizingCondenserConfig(
- max_size=50,
- keep_first=10,
- llm_config=LLMConfig(model='gpt-4o', api_key='test_key'),
- ),
- ]
- )
- condenser = Condenser.from_config(config, mock_llm_registry)
-
- assert isinstance(condenser, CondenserPipeline)
- assert len(condenser.condensers) == 3
- assert isinstance(condenser.condensers[0], NoOpCondenser)
- assert isinstance(condenser.condensers[1], BrowserOutputCondenser)
- assert isinstance(condenser.condensers[2], LLMSummarizingCondenser)
-
-
-def test_condenser_pipeline_chains_sub_condensers():
- """Test that the CondenserPipeline chains sub-condensers and combines their behavior."""
- MAX_SIZE = 10
- ATTENTION_WINDOW = 2
- NUMBER_OF_CONDENSATIONS = 3
-
- condenser = CondenserPipeline(
- AmortizedForgettingCondenser(max_size=MAX_SIZE),
- BrowserOutputCondenser(attention_window=ATTENTION_WINDOW),
- )
-
- harness = RollingCondenserTestHarness(condenser)
- events = [
- BrowserOutputObservation(
- f'Observation {i}', url='', trigger_by_action=ActionType.BROWSE
- )
- if i % 3 == 0
- else create_test_event(f'Event {i}')
- for i in range(0, MAX_SIZE * NUMBER_OF_CONDENSATIONS)
- ]
-
- for index, view in enumerate(harness.views(events)):
- # The amortized forgetting condenser is responsible for keeping the size
- # bounded despite the large number of events.
- assert len(view) == harness.expected_size(index, MAX_SIZE)
-
- # The browser output condenser should mask out the content of all the
- # browser observations outside the attention window (which is relative
- # to the number of browser outputs in the view, not the whole view or
- # the event stream).
- browser_outputs = [
- event
- for event in view
- if isinstance(
- event, (BrowserOutputObservation, AgentCondensationObservation)
- )
- ]
-
- for event in browser_outputs[:-ATTENTION_WINDOW]:
- assert 'Content omitted' in str(event)
-
- for event in browser_outputs[-ATTENTION_WINDOW:]:
- assert 'Content omitted' not in str(event)
diff --git a/tests/unit/memory/condenser/test_conversation_window_condenser.py b/tests/unit/memory/condenser/test_conversation_window_condenser.py
deleted file mode 100644
index 35eb198446..0000000000
--- a/tests/unit/memory/condenser/test_conversation_window_condenser.py
+++ /dev/null
@@ -1,571 +0,0 @@
-"""Unit tests for ConversationWindowCondenser.
-
-These tests mirror the tests for `_apply_conversation_window` in the AgentController,
-but adapted to test the condenser implementation. The ConversationWindowCondenser
-copies the functionality of the `_apply_conversation_window` function as closely as possible.
-
-The tests verify that the condenser:
-1. Identifies essential initial events (System Message, First User Message, Recall Action/Observation)
-2. Keeps roughly half of the non-essential events from recent history
-3. Handles dangling observations properly
-4. Returns appropriate CondensationAction objects specifying which events to forget
-"""
-
-from unittest.mock import patch
-
-import pytest
-
-from openhands.events import EventSource
-from openhands.events.action import CmdRunAction, MessageAction, RecallAction
-from openhands.events.action.agent import CondensationAction
-from openhands.events.action.message import SystemMessageAction
-from openhands.events.observation import (
- CmdOutputObservation,
- RecallObservation,
-)
-from openhands.events.recall_type import RecallType
-from openhands.memory.condenser.condenser import Condensation, View
-from openhands.memory.condenser.impl.conversation_window_condenser import (
- ConversationWindowCondenser,
-)
-
-
-# Helper function to create events with sequential IDs and causes
-def create_events(event_data):
- events = []
- # Import necessary types here to avoid repeated imports inside the loop
- from openhands.events.action import CmdRunAction, RecallAction
- from openhands.events.observation import CmdOutputObservation, RecallObservation
-
- for i, data in enumerate(event_data):
- event_type = data['type']
- source = data.get('source', EventSource.AGENT)
- kwargs = {} # Arguments for the event constructor
-
- # Determine arguments based on event type
- if event_type == RecallAction:
- kwargs['query'] = data.get('query', '')
- kwargs['recall_type'] = data.get('recall_type', RecallType.KNOWLEDGE)
- elif event_type == RecallObservation:
- kwargs['content'] = data.get('content', '')
- kwargs['recall_type'] = data.get('recall_type', RecallType.KNOWLEDGE)
- elif event_type == CmdRunAction:
- kwargs['command'] = data.get('command', '')
- elif event_type == CmdOutputObservation:
- # Required args for CmdOutputObservation
- kwargs['content'] = data.get('content', '')
- kwargs['command'] = data.get('command', '')
- # Pass command_id via kwargs if present in data
- if 'command_id' in data:
- kwargs['command_id'] = data['command_id']
- # Pass metadata if present
- if 'metadata' in data:
- kwargs['metadata'] = data['metadata']
- else: # Default for MessageAction, SystemMessageAction, etc.
- kwargs['content'] = data.get('content', '')
-
- # Instantiate the event
- event = event_type(**kwargs)
-
- # Assign internal attributes AFTER instantiation
- event._id = i + 1 # Assign sequential IDs starting from 1
- event._source = source
- # Assign _cause using cause_id from data, AFTER event._id is set
- if 'cause_id' in data:
- event._cause = data['cause_id']
- # If command_id was NOT passed via kwargs but cause_id exists,
- # pass cause_id as command_id to __init__ via kwargs for legacy handling
- # This needs to happen *before* instantiation if we want __init__ to handle it
- # Let's adjust the logic slightly:
- if event_type == CmdOutputObservation:
- if 'command_id' not in kwargs and 'cause_id' in data:
- kwargs['command_id'] = data['cause_id'] # Let __init__ handle this
- # Re-instantiate if we added command_id
- if 'command_id' in kwargs and event.command_id != kwargs['command_id']:
- event = event_type(**kwargs)
- event._id = i + 1
- event._source = source
-
- # Now assign _cause if it exists in data, after potential re-instantiation
- if 'cause_id' in data:
- event._cause = data['cause_id']
-
- events.append(event)
- return events
-
-
-def condenser_fixture():
- condenser = ConversationWindowCondenser()
- return condenser
-
-
-# =============================================
-# Test Cases for ConversationWindowCondenser
-# =============================================
-
-
-def test_basic_truncation(condenser_fixture):
- condenser = condenser_fixture
-
- events = create_events(
- [
- {'type': SystemMessageAction, 'content': 'System Prompt'}, # 1
- {
- 'type': MessageAction,
- 'content': 'User Task 1',
- 'source': EventSource.USER,
- }, # 2
- {'type': RecallAction, 'query': 'User Task 1'}, # 3
- {'type': RecallObservation, 'content': 'Recall result', 'cause_id': 3}, # 4
- {'type': CmdRunAction, 'command': 'ls'}, # 5
- {
- 'type': CmdOutputObservation,
- 'content': 'file1',
- 'command': 'ls',
- 'cause_id': 5,
- }, # 6
- {'type': CmdRunAction, 'command': 'pwd'}, # 7
- {
- 'type': CmdOutputObservation,
- 'content': '/dir',
- 'command': 'pwd',
- 'cause_id': 7,
- }, # 8
- {'type': CmdRunAction, 'command': 'cat file1'}, # 9
- {
- 'type': CmdOutputObservation,
- 'content': 'content',
- 'command': 'cat file1',
- 'cause_id': 9,
- }, # 10
- ]
- )
- view = View(events=events)
-
- # Calculation (RecallAction now essential):
- # History len = 10
- # Essentials = [sys(1), user(2), recall_act(3), recall_obs(4)] (len=4)
- # Non-essential count = 10 - 4 = 6
- # num_recent_to_keep = max(1, 6 // 2) = 3
- # slice_start_index = 10 - 3 = 7
- # recent_events_slice = history[7:] = [obs2(8), cmd3(9), obs3(10)]
- # Validation: remove leading obs2(8). validated_slice = [cmd3(9), obs3(10)]
- # Final = essentials + validated_slice = [sys(1), user(2), recall_act(3), recall_obs(4), cmd3(9), obs3(10)]
- # Expected IDs: [1, 2, 3, 4, 9, 10]. Length 6.
- # Forgotten IDs: [5, 6, 7, 8]
- condensation = condenser.get_condensation(view)
-
- assert isinstance(condensation, Condensation)
- assert isinstance(condensation.action, CondensationAction)
-
- # Check the forgotten event IDs
- forgotten_ids = condensation.action.forgotten
- expected_forgotten = [5, 6, 7, 8]
- assert sorted(forgotten_ids) == expected_forgotten
-
-
-def test_no_system_message(condenser_fixture):
- condenser = condenser_fixture
-
- events = create_events(
- [
- {
- 'type': MessageAction,
- 'content': 'User Task 1',
- 'source': EventSource.USER,
- }, # 1
- {'type': RecallAction, 'query': 'User Task 1'}, # 2
- {'type': RecallObservation, 'content': 'Recall result', 'cause_id': 2}, # 3
- {'type': CmdRunAction, 'command': 'ls'}, # 4
- {
- 'type': CmdOutputObservation,
- 'content': 'file1',
- 'command': 'ls',
- 'cause_id': 4,
- }, # 5
- {'type': CmdRunAction, 'command': 'pwd'}, # 6
- {
- 'type': CmdOutputObservation,
- 'content': '/dir',
- 'command': 'pwd',
- 'cause_id': 6,
- }, # 7
- {'type': CmdRunAction, 'command': 'cat file1'}, # 8
- {
- 'type': CmdOutputObservation,
- 'content': 'content',
- 'command': 'cat file1',
- 'cause_id': 8,
- }, # 9
- ]
- )
- view = View(events=events)
-
- # Calculation (RecallAction now essential):
- # History len = 9
- # Essentials = [user(1), recall_act(2), recall_obs(3)] (len=3)
- # Non-essential count = 9 - 3 = 6
- # num_recent_to_keep = max(1, 6 // 2) = 3
- # slice_start_index = 9 - 3 = 6
- # recent_events_slice = history[6:] = [obs2(7), cmd3(8), obs3(9)]
- # Validation: remove leading obs2(7). validated_slice = [cmd3(8), obs3(9)]
- # Final = essentials + validated_slice = [user(1), recall_act(2), recall_obs(3), cmd3(8), obs3(9)]
- # Expected kept IDs: [1, 2, 3, 8, 9]. Length 5.
- # Forgotten IDs: [4, 5, 6, 7]
- condensation = condenser.get_condensation(view)
-
- assert isinstance(condensation, Condensation)
- assert isinstance(condensation.action, CondensationAction)
-
- forgotten_ids = condensation.action.forgotten
- expected_forgotten = [4, 5, 6, 7]
- assert sorted(forgotten_ids) == expected_forgotten
-
-
-def test_no_recall_observation(condenser_fixture):
- condenser = condenser_fixture
-
- events = create_events(
- [
- {'type': SystemMessageAction, 'content': 'System Prompt'}, # 1
- {
- 'type': MessageAction,
- 'content': 'User Task 1',
- 'source': EventSource.USER,
- }, # 2
- {'type': RecallAction, 'query': 'User Task 1'}, # 3 (Recall Action exists)
- # Recall Observation is missing
- {'type': CmdRunAction, 'command': 'ls'}, # 4
- {
- 'type': CmdOutputObservation,
- 'content': 'file1',
- 'command': 'ls',
- 'cause_id': 4,
- }, # 5
- {'type': CmdRunAction, 'command': 'pwd'}, # 6
- {
- 'type': CmdOutputObservation,
- 'content': '/dir',
- 'command': 'pwd',
- 'cause_id': 6,
- }, # 7
- {'type': CmdRunAction, 'command': 'cat file1'}, # 8
- {
- 'type': CmdOutputObservation,
- 'content': 'content',
- 'command': 'cat file1',
- 'cause_id': 8,
- }, # 9
- ]
- )
- view = View(events=events)
-
- # Calculation (RecallAction essential even without RecallObs in condenser):
- # History len = 9
- # Essentials = [sys(1), user(2), recall_action(3)] (len=3)
- # Non-essential count = 9 - 3 = 6
- # num_recent_to_keep = max(1, 6 // 2) = 3
- # slice_start_index = 9 - 3 = 6
- # recent_events_slice = history[6:] = [obs2(7), cmd3(8), obs3(9)]
- # Validation: remove leading obs2(7). validated_slice = [cmd3(8), obs3(9)]
- # Final = essentials + validated_slice = [sys(1), user(2), recall_action(3), cmd_cat(8), obs_cat(9)]
- # Expected kept IDs: [1, 2, 3, 8, 9]. Length 5.
- # Forgotten IDs: [4, 5, 6, 7]
- condensation = condenser.get_condensation(view)
-
- assert isinstance(condensation, Condensation)
- assert isinstance(condensation.action, CondensationAction)
-
- forgotten_ids = condensation.action.forgotten
- expected_forgotten = [4, 5, 6, 7]
- assert sorted(forgotten_ids) == expected_forgotten
-
-
-def test_short_history_no_truncation(condenser_fixture):
- condenser = condenser_fixture
-
- events = create_events(
- [
- {'type': SystemMessageAction, 'content': 'System Prompt'}, # 1
- {
- 'type': MessageAction,
- 'content': 'User Task 1',
- 'source': EventSource.USER,
- }, # 2
- {'type': RecallAction, 'query': 'User Task 1'}, # 3
- {'type': RecallObservation, 'content': 'Recall result', 'cause_id': 3}, # 4
- {'type': CmdRunAction, 'command': 'ls'}, # 5
- {
- 'type': CmdOutputObservation,
- 'content': 'file1',
- 'command': 'ls',
- 'cause_id': 5,
- }, # 6
- ]
- )
- view = View(events=events)
-
- # Calculation (RecallAction now essential):
- # History len = 6
- # Essentials = [sys(1), user(2), recall_act(3), recall_obs(4)] (len=4)
- # Non-essential count = 6 - 4 = 2
- # num_recent_to_keep = max(1, 2 // 2) = 1
- # slice_start_index = 6 - 1 = 5
- # recent_events_slice = history[5:] = [obs1(6)]
- # Validation: remove leading obs1(6). validated_slice = []
- # Final = essentials + validated_slice = [sys(1), user(2), recall_act(3), recall_obs(4)]
- # Expected kept IDs: [1, 2, 3, 4]. Length 4.
- # Forgotten IDs: [5, 6]
- condensation = condenser.get_condensation(view)
-
- assert isinstance(condensation, Condensation)
- assert isinstance(condensation.action, CondensationAction)
-
- forgotten_ids = condensation.action.forgotten
- expected_forgotten = [5, 6]
- assert sorted(forgotten_ids) == expected_forgotten
-
-
-def test_only_essential_events(condenser_fixture):
- condenser = condenser_fixture
-
- events = create_events(
- [
- {'type': SystemMessageAction, 'content': 'System Prompt'}, # 1
- {
- 'type': MessageAction,
- 'content': 'User Task 1',
- 'source': EventSource.USER,
- }, # 2
- {'type': RecallAction, 'query': 'User Task 1'}, # 3
- {'type': RecallObservation, 'content': 'Recall result', 'cause_id': 3}, # 4
- ]
- )
- view = View(events=events)
-
- # Calculation (RecallAction now essential):
- # History len = 4
- # Essentials = [sys(1), user(2), recall_act(3), recall_obs(4)] (len=4)
- # Non-essential count = 4 - 4 = 0
- # num_recent_to_keep = max(1, 0 // 2) = 1
- # slice_start_index = 4 - 1 = 3
- # recent_events_slice = history[3:] = [recall_obs(4)]
- # Validation: remove leading recall_obs(4). validated_slice = []
- # Final = essentials + validated_slice = [sys(1), user(2), recall_act(3), recall_obs(4)]
- # Expected kept IDs: [1, 2, 3, 4]. Length 4.
- # Forgotten IDs: []
- condensation = condenser.get_condensation(view)
-
- assert isinstance(condensation, Condensation)
- assert isinstance(condensation.action, CondensationAction)
-
- forgotten_ids = condensation.action.forgotten
- expected_forgotten = []
- assert forgotten_ids == expected_forgotten
-
-
-def test_dangling_observations_at_cut_point(condenser_fixture):
- condenser = condenser_fixture
-
- events = create_events(
- [
- {'type': SystemMessageAction, 'content': 'System Prompt'}, # 1
- {
- 'type': MessageAction,
- 'content': 'User Task 1',
- 'source': EventSource.USER,
- }, # 2
- {'type': RecallAction, 'query': 'User Task 1'}, # 3
- {'type': RecallObservation, 'content': 'Recall result', 'cause_id': 3}, # 4
- # --- Slice calculation should start here ---
- {
- 'type': CmdOutputObservation,
- 'content': 'dangle1',
- 'command': 'cmd_unknown',
- }, # 5 (Dangling)
- {
- 'type': CmdOutputObservation,
- 'content': 'dangle2',
- 'command': 'cmd_unknown',
- }, # 6 (Dangling)
- {'type': CmdRunAction, 'command': 'cmd1'}, # 7
- {
- 'type': CmdOutputObservation,
- 'content': 'obs1',
- 'command': 'cmd1',
- 'cause_id': 7,
- }, # 8
- {'type': CmdRunAction, 'command': 'cmd2'}, # 9
- {
- 'type': CmdOutputObservation,
- 'content': 'obs2',
- 'command': 'cmd2',
- 'cause_id': 9,
- }, # 10
- ]
- ) # 10 events total
- view = View(events=events)
-
- # Calculation (RecallAction now essential):
- # History len = 10
- # Essentials = [sys(1), user(2), recall_act(3), recall_obs(4)] (len=4)
- # Non-essential count = 10 - 4 = 6
- # num_recent_to_keep = max(1, 6 // 2) = 3
- # slice_start_index = 10 - 3 = 7
- # recent_events_slice = history[7:] = [obs1(8), cmd2(9), obs2(10)]
- # Validation: remove leading obs1(8). validated_slice = [cmd2(9), obs2(10)]
- # Final = essentials + validated_slice = [sys(1), user(2), recall_act(3), recall_obs(4), cmd2(9), obs2(10)]
- # Expected kept IDs: [1, 2, 3, 4, 9, 10]. Length 6.
- # Forgotten IDs: [5, 6, 7, 8]
- condensation = condenser.get_condensation(view)
-
- assert isinstance(condensation, Condensation)
- assert isinstance(condensation.action, CondensationAction)
-
- forgotten_ids = condensation.action.forgotten
- expected_forgotten = [5, 6, 7, 8]
- assert sorted(forgotten_ids) == expected_forgotten
-
-
-def test_only_dangling_observations_in_recent_slice(condenser_fixture):
- condenser = condenser_fixture
-
- events = create_events(
- [
- {'type': SystemMessageAction, 'content': 'System Prompt'}, # 1
- {
- 'type': MessageAction,
- 'content': 'User Task 1',
- 'source': EventSource.USER,
- }, # 2
- {'type': RecallAction, 'query': 'User Task 1'}, # 3
- {'type': RecallObservation, 'content': 'Recall result', 'cause_id': 3}, # 4
- # --- Slice calculation should start here ---
- {
- 'type': CmdOutputObservation,
- 'content': 'dangle1',
- 'command': 'cmd_unknown',
- }, # 5 (Dangling)
- {
- 'type': CmdOutputObservation,
- 'content': 'dangle2',
- 'command': 'cmd_unknown',
- }, # 6 (Dangling)
- ]
- ) # 6 events total
- view = View(events=events)
-
- # Calculation (RecallAction now essential):
- # History len = 6
- # Essentials = [sys(1), user(2), recall_act(3), recall_obs(4)] (len=4)
- # Non-essential count = 6 - 4 = 2
- # num_recent_to_keep = max(1, 2 // 2) = 1
- # slice_start_index = 6 - 1 = 5
- # recent_events_slice = history[5:] = [dangle2(6)]
- # Validation: remove leading dangle2(6). validated_slice = [] (Corrected based on user feedback/bugfix)
- # Final = essentials + validated_slice = [sys(1), user(2), recall_act(3), recall_obs(4)]
- # Expected kept IDs: [1, 2, 3, 4]. Length 4.
- # Forgotten IDs: [5, 6]
- with patch(
- 'openhands.memory.condenser.impl.conversation_window_condenser.logger.warning'
- ) as mock_log_warning:
- condensation = condenser.get_condensation(view)
-
- assert isinstance(condensation, Condensation)
- assert isinstance(condensation.action, CondensationAction)
-
- forgotten_ids = condensation.action.forgotten
- expected_forgotten = [5, 6]
- assert sorted(forgotten_ids) == expected_forgotten
-
- # Check that the specific warning was logged exactly once
- assert mock_log_warning.call_count == 1
-
- # Check the essential parts of the arguments
- call_args, call_kwargs = mock_log_warning.call_args
- expected_message_substring = 'All recent events are dangling observations, which we truncate. This means the agent has only the essential first events. This should not happen.'
- assert expected_message_substring in call_args[0]
-
-
-def test_empty_history(condenser_fixture):
- condenser = condenser_fixture
- view = View(events=[])
-
- condensation = condenser.get_condensation(view)
-
- assert isinstance(condensation, Condensation)
- assert isinstance(condensation.action, CondensationAction)
- assert condensation.action.forgotten == []
-
-
-def test_multiple_user_messages(condenser_fixture):
- condenser = condenser_fixture
-
- events = create_events(
- [
- {'type': SystemMessageAction, 'content': 'System Prompt'}, # 1
- {
- 'type': MessageAction,
- 'content': 'User Task 1',
- 'source': EventSource.USER,
- }, # 2 (First)
- {'type': RecallAction, 'query': 'User Task 1'}, # 3
- {
- 'type': RecallObservation,
- 'content': 'Recall result 1',
- 'cause_id': 3,
- }, # 4
- {'type': CmdRunAction, 'command': 'cmd1'}, # 5
- {
- 'type': CmdOutputObservation,
- 'content': 'obs1',
- 'command': 'cmd1',
- 'cause_id': 5,
- }, # 6
- {
- 'type': MessageAction,
- 'content': 'User Task 2',
- 'source': EventSource.USER,
- }, # 7 (Second)
- {'type': RecallAction, 'query': 'User Task 2'}, # 8
- {
- 'type': RecallObservation,
- 'content': 'Recall result 2',
- 'cause_id': 8,
- }, # 9
- {'type': CmdRunAction, 'command': 'cmd2'}, # 10
- {
- 'type': CmdOutputObservation,
- 'content': 'obs2',
- 'command': 'cmd2',
- 'cause_id': 10,
- }, # 11
- ]
- ) # 11 events total
- view = View(events=events)
-
- # Calculation (RecallAction now essential):
- # History len = 11
- # Essentials = [sys(1), user1(2), recall_act1(3), recall_obs1(4)] (len=4)
- # Non-essential count = 11 - 4 = 7
- # num_recent_to_keep = max(1, 7 // 2) = 3
- # slice_start_index = 11 - 3 = 8
- # recent_events_slice = history[8:] = [recall_obs2(9), cmd2(10), obs2(11)]
- # Validation: remove leading recall_obs2(9). validated_slice = [cmd2(10), obs2(11)]
- # Final = essentials + validated_slice = [sys(1), user1(2), recall_act1(3), recall_obs1(4)] + [cmd2(10), obs2(11)]
- # Expected kept IDs: [1, 2, 3, 4, 10, 11]. Length 6.
- # Forgotten IDs: [5, 6, 7, 8, 9]
- condensation = condenser.get_condensation(view)
-
- assert isinstance(condensation, Condensation)
- assert isinstance(condensation.action, CondensationAction)
-
- forgotten_ids = condensation.action.forgotten
- expected_forgotten = [5, 6, 7, 8, 9]
- assert sorted(forgotten_ids) == expected_forgotten
-
- # Additional validation: ensure that only the first user message is kept
- kept_event_ids = set(range(1, 12)) - set(forgotten_ids)
- assert 2 in kept_event_ids # First user message kept
- assert 7 not in kept_event_ids # Second user message forgotten
diff --git a/tests/unit/memory/test_view.py b/tests/unit/memory/test_view.py
deleted file mode 100644
index c68b26c3ac..0000000000
--- a/tests/unit/memory/test_view.py
+++ /dev/null
@@ -1,267 +0,0 @@
-from openhands.events.action.agent import CondensationAction, CondensationRequestAction
-from openhands.events.action.message import MessageAction
-from openhands.events.event import Event
-from openhands.events.observation.agent import AgentCondensationObservation
-from openhands.memory.view import View
-
-
-def test_view_preserves_uncondensed_lists() -> None:
- """Tests that the view preserves event lists that don't contain condensation actions."""
- events: list[Event] = [MessageAction(content=f'Event {i}') for i in range(5)]
- set_ids(events)
- view = View.from_events(events)
- assert len(view) == 5
- assert view.events == events
-
-
-def test_view_forgets_events() -> None:
- """Tests that views drop forgotten events and the condensation actions."""
- events: list[Event] = [
- *[MessageAction(content=f'Event {i}') for i in range(5)],
- CondensationAction(forgotten_event_ids=[0, 1, 2, 3, 4]),
- ]
- set_ids(events)
-
- view = View.from_events(events)
- assert view.events == [] # All events forgotten and condensation action removed
-
-
-def test_view_keeps_non_forgotten_events() -> None:
- """Tests that views keep non-forgotten events."""
- for forgotten_event_id in range(5):
- events: list[Event] = [
- *[MessageAction(content=f'Event {i}') for i in range(5)],
- # Instead of forgetting all events like in
- # `test_view_forgets_events`, in this test we only want to forget
- # one of the events. That way we can check that the rest of the
- # events are preserved.
- CondensationAction(forgotten_event_ids=[forgotten_event_id]),
- ]
- set_ids(events)
-
- view = View.from_events(events)
- assert (
- view.events
- == events[:forgotten_event_id] + events[(forgotten_event_id + 1) : 5]
- )
-
-
-def test_view_inserts_summary() -> None:
- """Tests that views insert a summary observation at the specified offset."""
- for offset in range(5):
- events: list[Event] = [
- *[MessageAction(content=f'Event {i}') for i in range(5)],
- CondensationAction(
- forgotten_event_ids=[], summary='My Summary', summary_offset=offset
- ),
- ]
- set_ids(events)
- view = View.from_events(events)
-
- assert len(view) == 6 # 5 message events + 1 summary observation
- for index, event in enumerate(view):
- print(index, event.content)
- if index == offset:
- assert isinstance(event, AgentCondensationObservation)
- assert event.content == 'My Summary'
-
- # Events before where the summary is inserted will have content
- # matching their index.
- elif index < offset:
- assert isinstance(event, MessageAction)
- assert event.content == f'Event {index}'
-
- # Events after where the summary is inserted will be offset by one
- # from the original list.
- else:
- assert isinstance(event, MessageAction)
- assert event.content == f'Event {index - 1}'
-
-
-def test_no_condensation_action_in_view() -> None:
- """Ensure that CondensationAction events are never present in the resulting view."""
- events: list[Event] = [
- MessageAction(content='Event 0'),
- MessageAction(content='Event 1'),
- CondensationAction(forgotten_event_ids=[0]),
- MessageAction(content='Event 2'),
- MessageAction(content='Event 3'),
- ]
- set_ids(events)
- view = View.from_events(events)
-
- # Check that no CondensationAction is present in the view
- for event in view:
- assert not isinstance(event, CondensationAction)
-
- # The view should only contain the non-forgotten MessageActions
- assert len(view) == 3 # Event 1, Event 2, Event 3 (Event 0 was forgotten)
-
-
-def test_unhandled_condensation_request_with_no_condensation() -> None:
- """Test that unhandled_condensation_request is True when there's a CondensationRequestAction but no CondensationAction."""
- events: list[Event] = [
- MessageAction(content='Event 0'),
- MessageAction(content='Event 1'),
- CondensationRequestAction(),
- MessageAction(content='Event 2'),
- ]
- set_ids(events)
- view = View.from_events(events)
-
- # Should be marked as having an unhandled condensation request
- assert view.unhandled_condensation_request is True
-
- # CondensationRequestAction should be removed from the view
- assert len(view) == 3 # Only the MessageActions remain
- for event in view:
- assert not isinstance(event, CondensationRequestAction)
-
-
-def test_handled_condensation_request_with_condensation_action() -> None:
- """Test that unhandled_condensation_request is False when CondensationAction comes after CondensationRequestAction."""
- events: list[Event] = [
- MessageAction(content='Event 0'),
- MessageAction(content='Event 1'),
- CondensationRequestAction(),
- MessageAction(content='Event 2'),
- CondensationAction(forgotten_event_ids=[0, 1]), # Handles the request
- MessageAction(content='Event 3'),
- ]
- set_ids(events)
- view = View.from_events(events)
-
- # Should NOT be marked as having an unhandled condensation request
- assert view.unhandled_condensation_request is False
-
- # Both CondensationRequestAction and CondensationAction should be removed from the view
- assert len(view) == 2 # Event 2 and Event 3 (Event 0, 1 forgotten)
- for event in view:
- assert not isinstance(event, CondensationRequestAction)
- assert not isinstance(event, CondensationAction)
-
-
-def test_multiple_condensation_requests_pattern() -> None:
- """Test the pattern with multiple condensation requests and actions."""
- events: list[Event] = [
- MessageAction(content='Event 0'),
- CondensationRequestAction(), # First request
- MessageAction(content='Event 1'),
- CondensationAction(forgotten_event_ids=[0]), # Handles first request
- MessageAction(content='Event 2'),
- CondensationRequestAction(), # Second request - should be unhandled
- MessageAction(content='Event 3'),
- ]
- set_ids(events)
- view = View.from_events(events)
-
- # Should be marked as having an unhandled condensation request (the second one)
- assert view.unhandled_condensation_request is True
-
- # Both CondensationRequestActions and CondensationAction should be removed from the view
- assert len(view) == 3 # Event 1, Event 2, Event 3 (Event 0 forgotten)
- for event in view:
- assert not isinstance(event, CondensationRequestAction)
- assert not isinstance(event, CondensationAction)
-
-
-def test_condensation_action_before_request() -> None:
- """Test that CondensationAction before CondensationRequestAction doesn't affect the unhandled status."""
- events: list[Event] = [
- MessageAction(content='Event 0'),
- CondensationAction(
- forgotten_event_ids=[]
- ), # This doesn't handle the later request
- MessageAction(content='Event 1'),
- CondensationRequestAction(), # This should be unhandled
- MessageAction(content='Event 2'),
- ]
- set_ids(events)
- view = View.from_events(events)
-
- # Should be marked as having an unhandled condensation request
- assert view.unhandled_condensation_request is True
-
- # Both CondensationRequestAction and CondensationAction should be removed from the view
- assert len(view) == 3 # Event 0, Event 1, Event 2
- for event in view:
- assert not isinstance(event, CondensationRequestAction)
- assert not isinstance(event, CondensationAction)
-
-
-def test_no_condensation_events() -> None:
- """Test that unhandled_condensation_request is False when there are no condensation events."""
- events: list[Event] = [
- MessageAction(content='Event 0'),
- MessageAction(content='Event 1'),
- MessageAction(content='Event 2'),
- ]
- set_ids(events)
- view = View.from_events(events)
-
- # Should NOT be marked as having an unhandled condensation request
- assert view.unhandled_condensation_request is False
-
- # All events should remain
- assert len(view) == 3
- assert view.events == events
-
-
-def test_only_condensation_action() -> None:
- """Test behavior when there's only a CondensationAction (no request)."""
- events: list[Event] = [
- MessageAction(content='Event 0'),
- MessageAction(content='Event 1'),
- CondensationAction(forgotten_event_ids=[0]),
- MessageAction(content='Event 2'),
- ]
- set_ids(events)
- view = View.from_events(events)
-
- # Should NOT be marked as having an unhandled condensation request
- assert view.unhandled_condensation_request is False
-
- # CondensationAction should be removed, Event 0 should be forgotten
- assert len(view) == 2 # Event 1, Event 2
- for event in view:
- assert not isinstance(event, CondensationAction)
-
-
-def test_condensation_request_always_removed_from_view() -> None:
- """Test that CondensationRequestAction is always removed from the view regardless of unhandled status."""
- # Test case 1: Unhandled request
- events_unhandled: list[Event] = [
- MessageAction(content='Event 0'),
- CondensationRequestAction(),
- MessageAction(content='Event 1'),
- ]
- set_ids(events_unhandled)
- view_unhandled = View.from_events(events_unhandled)
-
- assert view_unhandled.unhandled_condensation_request is True
- assert len(view_unhandled) == 2 # Only MessageActions
- for event in view_unhandled:
- assert not isinstance(event, CondensationRequestAction)
-
- # Test case 2: Handled request
- events_handled: list[Event] = [
- MessageAction(content='Event 0'),
- CondensationRequestAction(),
- MessageAction(content='Event 1'),
- CondensationAction(forgotten_event_ids=[]),
- MessageAction(content='Event 2'),
- ]
- set_ids(events_handled)
- view_handled = View.from_events(events_handled)
-
- assert view_handled.unhandled_condensation_request is False
- assert len(view_handled) == 3 # Only MessageActions
- for event in view_handled:
- assert not isinstance(event, CondensationRequestAction)
- assert not isinstance(event, CondensationAction)
-
-
-def set_ids(events: list[Event]) -> None:
- """Set the IDs of the events in the list to their index."""
- for i, e in enumerate(events):
- e._id = i # type: ignore
diff --git a/tests/unit/microagent/test_user_microagents.py b/tests/unit/microagent/test_user_microagents.py
deleted file mode 100644
index b30f1cd031..0000000000
--- a/tests/unit/microagent/test_user_microagents.py
+++ /dev/null
@@ -1,217 +0,0 @@
-"""Tests for user directory microagent loading."""
-
-import tempfile
-from pathlib import Path
-from unittest.mock import patch
-
-import pytest
-
-from openhands.events.stream import EventStream
-from openhands.memory.memory import Memory
-from openhands.microagent import KnowledgeMicroagent, MicroagentType, RepoMicroagent
-from openhands.storage import get_file_store
-
-
-def temp_user_microagents_dir():
- """Create a temporary directory to simulate ~/.openhands/microagents/."""
- with tempfile.TemporaryDirectory() as temp_dir:
- user_dir = Path(temp_dir)
-
- # Create test knowledge agent
- knowledge_agent = """---
-name: user_knowledge
-version: 1.0.0
-agent: CodeActAgent
-triggers:
- - user-test
- - personal
----
-
-# User Knowledge Agent
-
-Personal knowledge and guidelines.
-"""
- (user_dir / 'user_knowledge.md').write_text(knowledge_agent)
-
- # Create test repo agent
- repo_agent = """---
-name: user_repo
-version: 1.0.0
-agent: CodeActAgent
----
-
-# User Repository Agent
-
-Personal repository-specific instructions.
-"""
- (user_dir / 'user_repo.md').write_text(repo_agent)
-
- yield user_dir
-
-
-def test_user_microagents_loading(temp_user_microagents_dir):
- """Test that user microagents are loaded from ~/.openhands/microagents/."""
- with patch(
- 'openhands.memory.memory.USER_MICROAGENTS_DIR', str(temp_user_microagents_dir)
- ):
- with tempfile.TemporaryDirectory() as temp_dir:
- # Create event stream and memory
- file_store = get_file_store('local', temp_dir)
- event_stream = EventStream('test', file_store)
- memory = Memory(event_stream, 'test_sid')
-
- # Check that user microagents were loaded
- assert 'user_knowledge' in memory.knowledge_microagents
- assert 'user_repo' in memory.repo_microagents
-
- # Verify the loaded agents
- user_knowledge = memory.knowledge_microagents['user_knowledge']
- assert isinstance(user_knowledge, KnowledgeMicroagent)
- assert user_knowledge.type == MicroagentType.KNOWLEDGE
- assert 'user-test' in user_knowledge.triggers
- assert 'personal' in user_knowledge.triggers
-
- user_repo = memory.repo_microagents['user_repo']
- assert isinstance(user_repo, RepoMicroagent)
- assert user_repo.type == MicroagentType.REPO_KNOWLEDGE
-
-
-def test_user_microagents_directory_creation():
- """Test that user microagents directory is created if it doesn't exist."""
- with tempfile.TemporaryDirectory() as temp_dir:
- non_existent_dir = Path(temp_dir) / 'non_existent' / 'microagents'
-
- with patch(
- 'openhands.memory.memory.USER_MICROAGENTS_DIR', str(non_existent_dir)
- ):
- with tempfile.TemporaryDirectory() as temp_store_dir:
- # Create event stream and memory
- file_store = get_file_store('local', temp_store_dir)
- event_stream = EventStream('test', file_store)
- Memory(event_stream, 'test_sid')
-
- # Check that the directory was created
- assert non_existent_dir.exists()
- assert non_existent_dir.is_dir()
-
-
-def test_user_microagents_override_global():
- """Test that user microagents can override global ones with the same name."""
- with tempfile.TemporaryDirectory() as temp_dir:
- user_dir = Path(temp_dir)
-
- # Create a user microagent with the same name as a global one
- # (assuming there's a global 'github' microagent)
- github_agent = """---
-name: github
-version: 1.0.0
-agent: CodeActAgent
-triggers:
- - github
- - git
----
-
-# Personal GitHub Agent
-
-My personal GitHub workflow and preferences.
-"""
- (user_dir / 'github.md').write_text(github_agent)
-
- with patch('openhands.memory.memory.USER_MICROAGENTS_DIR', str(user_dir)):
- with tempfile.TemporaryDirectory() as temp_store_dir:
- # Create event stream and memory
- file_store = get_file_store('local', temp_store_dir)
- event_stream = EventStream('test', file_store)
- memory = Memory(event_stream, 'test_sid')
-
- # Check that the user microagent is loaded
- if 'github' in memory.knowledge_microagents:
- github_microagent = memory.knowledge_microagents['github']
- # The user version should contain our personal content
- assert 'My personal GitHub workflow' in github_microagent.content
-
-
-def test_user_microagents_loading_error_handling():
- """Test error handling when user microagents directory has issues."""
- with tempfile.TemporaryDirectory() as temp_dir:
- user_dir = Path(temp_dir)
-
- # Create an invalid microagent file
- invalid_agent = """---
-name: invalid
-type: invalid_type
----
-
-# Invalid Agent
-"""
- (user_dir / 'invalid.md').write_text(invalid_agent)
-
- with patch('openhands.memory.memory.USER_MICROAGENTS_DIR', str(user_dir)):
- with tempfile.TemporaryDirectory() as temp_store_dir:
- # Create event stream and memory - should not crash
- file_store = get_file_store('local', temp_store_dir)
- event_stream = EventStream('test', file_store)
- memory = Memory(event_stream, 'test_sid')
-
- # Memory should still be created despite the invalid microagent
- assert memory is not None
- # The invalid microagent should not be loaded
- assert 'invalid' not in memory.knowledge_microagents
- assert 'invalid' not in memory.repo_microagents
-
-
-def test_user_microagents_empty_directory():
- """Test behavior when user microagents directory is empty."""
- with tempfile.TemporaryDirectory() as temp_dir:
- empty_dir = Path(temp_dir)
-
- with patch('openhands.memory.memory.USER_MICROAGENTS_DIR', str(empty_dir)):
- with tempfile.TemporaryDirectory() as temp_store_dir:
- # Create event stream and memory
- file_store = get_file_store('local', temp_store_dir)
- event_stream = EventStream('test', file_store)
- memory = Memory(event_stream, 'test_sid')
-
- # Memory should be created successfully
- assert memory is not None
- # No user microagents should be loaded, but global ones might be
- # (we can't assert the exact count since global microagents may exist)
-
-
-def test_user_microagents_nested_directories(temp_user_microagents_dir):
- """Test loading user microagents from nested directories."""
- # Create nested microagent
- nested_dir = temp_user_microagents_dir / 'personal' / 'tools'
- nested_dir.mkdir(parents=True)
-
- nested_agent = """---
-name: personal_tool
-version: 1.0.0
-agent: CodeActAgent
-triggers:
- - personal-tool
----
-
-# Personal Tool Agent
-
-My personal development tools and workflows.
-"""
- (nested_dir / 'tool.md').write_text(nested_agent)
-
- with patch(
- 'openhands.memory.memory.USER_MICROAGENTS_DIR', str(temp_user_microagents_dir)
- ):
- with tempfile.TemporaryDirectory() as temp_store_dir:
- # Create event stream and memory
- file_store = get_file_store('local', temp_store_dir)
- event_stream = EventStream('test', file_store)
- memory = Memory(event_stream, 'test_sid')
-
- # Check that nested microagent was loaded
- # The name should be derived from the relative path
- assert 'personal/tools/tool' in memory.knowledge_microagents
-
- nested_microagent = memory.knowledge_microagents['personal/tools/tool']
- assert isinstance(nested_microagent, KnowledgeMicroagent)
- assert 'personal-tool' in nested_microagent.triggers