Condensation request signal in event stream (#9097)

Co-authored-by: Calvin Smith <calvin@all-hands.dev>
This commit is contained in:
Calvin Smith 2025-06-27 09:57:39 -06:00 committed by GitHub
parent b74da7d4c3
commit 04a15b1467
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
18 changed files with 663 additions and 557 deletions

View File

@ -260,6 +260,9 @@ enable_finish = true
# length limit
enable_history_truncation = true
# Whether the condensation request tool is enabled
enable_condensation_request = false
[agent.RepoExplorerAgent]
# Example: use a cheaper model for RepoExplorerAgent to reduce cost, especially
# useful when an agent doesn't demand high quality but uses a lot of tokens

View File

@ -12,6 +12,9 @@ if TYPE_CHECKING:
import openhands.agenthub.codeact_agent.function_calling as codeact_function_calling
from openhands.agenthub.codeact_agent.tools.bash import create_cmd_run_tool
from openhands.agenthub.codeact_agent.tools.browser import BrowserTool
from openhands.agenthub.codeact_agent.tools.condensation_request import (
CondensationRequestTool,
)
from openhands.agenthub.codeact_agent.tools.finish import FinishTool
from openhands.agenthub.codeact_agent.tools.ipython import IPythonTool
from openhands.agenthub.codeact_agent.tools.llm_based_edit import LLMBasedFileEditTool
@ -119,6 +122,8 @@ class CodeActAgent(Agent):
tools.append(ThinkTool)
if self.config.enable_finish:
tools.append(FinishTool)
if self.config.enable_condensation_request:
tools.append(CondensationRequestTool)
if self.config.enable_browsing:
if sys.platform == 'win32':
logger.warning('Windows runtime does not support browsing yet')

View File

@ -11,6 +11,7 @@ from litellm import (
from openhands.agenthub.codeact_agent.tools import (
BrowserTool,
CondensationRequestTool,
FinishTool,
IPythonTool,
LLMBasedFileEditTool,
@ -35,6 +36,7 @@ from openhands.events.action import (
IPythonRunCellAction,
MessageAction,
)
from openhands.events.action.agent import CondensationRequestAction
from openhands.events.action.mcp import MCPAction
from openhands.events.event import FileEditSource, FileReadSource
from openhands.events.tool import ToolCallMetadata
@ -203,6 +205,12 @@ def response_to_actions(
elif tool_call.function.name == ThinkTool['function']['name']:
action = AgentThinkAction(thought=arguments.get('thought', ''))
# ================================================
# CondensationRequestAction
# ================================================
elif tool_call.function.name == CondensationRequestTool['function']['name']:
action = CondensationRequestAction()
# ================================================
# BrowserTool
# ================================================

View File

@ -1,5 +1,6 @@
from .bash import create_cmd_run_tool
from .browser import BrowserTool
from .condensation_request import CondensationRequestTool
from .finish import FinishTool
from .ipython import IPythonTool
from .llm_based_edit import LLMBasedFileEditTool
@ -8,6 +9,7 @@ from .think import ThinkTool
__all__ = [
'BrowserTool',
'CondensationRequestTool',
'create_cmd_run_tool',
'FinishTool',
'IPythonTool',

View File

@ -0,0 +1,16 @@
from litellm import ChatCompletionToolParam, ChatCompletionToolParamFunctionChunk
_CONDENSATION_REQUEST_DESCRIPTION = 'Request a condensation of the conversation history when the context becomes too long or when you need to focus on the most relevant information.'
CondensationRequestTool = ChatCompletionToolParam(
type='function',
function=ChatCompletionToolParamFunctionChunk(
name='request_condensation',
description=_CONDENSATION_REQUEST_DESCRIPTION,
parameters={
'type': 'object',
'properties': {},
'required': [],
},
),
)

View File

@ -59,7 +59,11 @@ from openhands.events.action import (
NullAction,
SystemMessageAction,
)
from openhands.events.action.agent import CondensationAction, RecallAction
from openhands.events.action.agent import (
CondensationAction,
CondensationRequestAction,
RecallAction,
)
from openhands.events.event import Event
from openhands.events.observation import (
AgentDelegateObservation,
@ -71,7 +75,6 @@ from openhands.events.observation import (
from openhands.events.serialization.event import truncate_content
from openhands.llm.llm import LLM
from openhands.llm.metrics import Metrics
from openhands.memory.view import View
from openhands.storage.files import FileStore
# note: RESUME is only available on web GUI
@ -336,6 +339,8 @@ class AgentController:
return True
if isinstance(event, CondensationAction):
return True
if isinstance(event, CondensationRequestAction):
return True
return False
if isinstance(event, Observation):
if (
@ -829,7 +834,9 @@ class AgentController:
or isinstance(e, ContextWindowExceededError)
):
if self.agent.config.enable_history_truncation:
self._handle_long_context_error()
self.event_stream.add_event(
CondensationRequestAction(), EventSource.AGENT
)
return
else:
raise LLMContextWindowExceedError()
@ -949,180 +956,6 @@ class AgentController:
assert self._closed
return self.state_tracker.get_trajectory(include_screenshots)
def _handle_long_context_error(self) -> None:
# When context window is exceeded, keep roughly half of agent interactions
current_view = View.from_events(self.state.history)
kept_events = self._apply_conversation_window(current_view.events)
kept_event_ids = {e.id for e in kept_events}
self.log(
'info',
f'Context window exceeded. Keeping events with IDs: {kept_event_ids}',
)
# The events to forget are those that are not in the kept set
forgotten_event_ids = {e.id for e in self.state.history} - kept_event_ids
if len(kept_event_ids) == 0:
self.log(
'warning',
'No events kept after applying conversation window. This should not happen.',
)
# verify that the first event id in kept_event_ids is the same as the start_id
if len(kept_event_ids) > 0 and self.state.history[0].id not in kept_event_ids:
self.log(
'warning',
f'First event after applying conversation window was not kept: {self.state.history[0].id} not in {kept_event_ids}',
)
# Add an error event to trigger another step by the agent
self.event_stream.add_event(
CondensationAction(
forgotten_events_start_id=min(forgotten_event_ids)
if forgotten_event_ids
else 0,
forgotten_events_end_id=max(forgotten_event_ids)
if forgotten_event_ids
else 0,
),
EventSource.AGENT,
)
def _apply_conversation_window(self, history: list[Event]) -> list[Event]:
"""Cuts history roughly in half when context window is exceeded.
It preserves action-observation pairs and ensures that the system message,
the first user message, and its associated recall observation are always included
at the beginning of the context window.
The algorithm:
1. Identify essential initial events: System Message, First User Message, Recall Observation.
2. Determine the slice of recent events to potentially keep.
3. Validate the start of the recent slice for dangling observations.
4. Combine essential events and validated recent events, ensuring essentials come first.
Args:
events: List of events to filter
Returns:
Filtered list of events keeping newest half while preserving pairs and essential initial events.
"""
# Handle empty history
if not history:
return []
# 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 history if isinstance(e, SystemMessageAction)), None
)
assert (
system_message is None
or isinstance(system_message, SystemMessageAction)
and system_message.id == history[0].id
)
# Find First User Message in the history, which MUST exist
first_user_msg = self._first_user_message(history)
if first_user_msg is None:
# If not found in history, try the event stream
first_user_msg = self._first_user_message()
if first_user_msg is None:
raise RuntimeError('No first user message found in the event stream.')
self.log(
'warning',
'First user message not found in history. Using cached version from event stream.',
)
# Find the first user message index in the history
first_user_msg_index = -1
for i, event in enumerate(history):
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
# Look for RecallAction after the first user message
for i in range(first_user_msg_index + 1, len(history)):
event = history[i]
if (
isinstance(event, RecallAction)
and event.query == first_user_msg.content
):
# Found RecallAction, now look for its Observation
recall_action = event
for j in range(i + 1, len(history)):
obs_event = history[j]
# Check for Observation caused by this RecallAction
if (
isinstance(obs_event, Observation)
and obs_event.cause == recall_action.id
):
recall_observation = obs_event
break # Found the observation, stop inner loop
break # Found the recall action (and maybe obs), stop outer loop
essential_events: list[Event] = []
if system_message:
essential_events.append(system_message)
# Only include first user message if history is not empty
if history:
essential_events.append(first_user_msg)
# Include recall action and observation if both exist
if recall_action and recall_observation:
essential_events.append(recall_action)
essential_events.append(recall_observation)
# Include recall action without observation for backward compatibility
elif recall_action:
essential_events.append(recall_action)
# 2. Determine the slice of recent events to potentially keep
num_non_essential_events = len(history) - len(essential_events)
# Keep roughly half of the non-essential events, minimum 1
num_recent_to_keep = max(1, num_non_essential_events // 2)
# Calculate the starting index for the recent slice
slice_start_index = len(history) - num_recent_to_keep
slice_start_index = max(0, slice_start_index) # Ensure index is not negative
recent_events_slice = history[slice_start_index:]
# 3. Validate the start of the recent slice for dangling observations
# IMPORTANT: Most observations in history are tool call results, which cannot be without their action, or we get an LLM API error
first_valid_event_index = 0
for i, event in enumerate(recent_events_slice):
if isinstance(event, Observation):
first_valid_event_index += 1
else:
break
# If all events in the slice are dangling observations, we need to keep at least one
if first_valid_event_index == len(recent_events_slice):
self.log(
'warning',
'All recent events are dangling observations, which we truncate. This means the agent has only the essential first events. This should not happen.',
)
# Adjust the recent_events_slice if dangling observations were found at the start
if first_valid_event_index < len(recent_events_slice):
validated_recent_events = recent_events_slice[first_valid_event_index:]
if first_valid_event_index > 0:
self.log(
'debug',
f'Removed {first_valid_event_index} dangling observation(s) from the start of recent event slice.',
)
else:
validated_recent_events = []
# 4. Combine essential events and validated recent events
events_to_keep: list[Event] = essential_events + validated_recent_events
self.log('debug', f'History truncated. Kept {len(events_to_keep)} events.')
return events_to_keep
def _is_stuck(self) -> bool:
"""Checks if the agent or its delegate is stuck in a loop.

View File

@ -31,6 +31,8 @@ class AgentConfig(BaseModel):
"""Whether to enable think tool"""
enable_finish: bool = Field(default=True)
"""Whether to enable finish tool"""
enable_condensation_request: bool = Field(default=False)
"""Whether to enable condensation request tool"""
enable_prompt_extensions: bool = Field(default=True)
"""Whether to enable prompt extensions"""
enable_mcp: bool = Field(default=True)
@ -51,8 +53,7 @@ class AgentConfig(BaseModel):
@classmethod
def from_toml_section(cls, data: dict) -> dict[str, AgentConfig]:
"""
Create a mapping of AgentConfig instances from a toml dictionary representing the [agent] section.
"""Create a mapping of AgentConfig instances from a toml dictionary representing the [agent] section.
The default configuration is built from all non-dict keys in data.
Then, each key with a dict value is treated as a custom agent configuration, and its values override
@ -70,7 +71,6 @@ class AgentConfig(BaseModel):
dict[str, AgentConfig]: A mapping where the key "agent" corresponds to the default configuration
and additional keys represent custom configurations.
"""
# Initialize the result mapping
agent_mapping: dict[str, AgentConfig] = {}

View File

@ -11,7 +11,7 @@ from openhands.core.config.llm_config import LLMConfig
class NoOpCondenserConfig(BaseModel):
"""Configuration for NoOpCondenser."""
type: Literal['noop'] = 'noop'
type: Literal['noop'] = Field(default='noop')
model_config = ConfigDict(extra='forbid')
@ -19,7 +19,7 @@ class NoOpCondenserConfig(BaseModel):
class ObservationMaskingCondenserConfig(BaseModel):
"""Configuration for ObservationMaskingCondenser."""
type: Literal['observation_masking'] = 'observation_masking'
type: Literal['observation_masking'] = Field(default='observation_masking')
attention_window: int = Field(
default=100,
description='The number of most-recent events where observations will not be masked.',
@ -32,7 +32,7 @@ class ObservationMaskingCondenserConfig(BaseModel):
class BrowserOutputCondenserConfig(BaseModel):
"""Configuration for the BrowserOutputCondenser."""
type: Literal['browser_output_masking'] = 'browser_output_masking'
type: Literal['browser_output_masking'] = Field(default='browser_output_masking')
attention_window: int = Field(
default=1,
description='The number of most recent browser output observations that will not be masked.',
@ -43,7 +43,7 @@ class BrowserOutputCondenserConfig(BaseModel):
class RecentEventsCondenserConfig(BaseModel):
"""Configuration for RecentEventsCondenser."""
type: Literal['recent'] = 'recent'
type: Literal['recent'] = Field(default='recent')
# at least one event by default, because the best guess is that it is the user task
keep_first: int = Field(
@ -61,7 +61,7 @@ class RecentEventsCondenserConfig(BaseModel):
class LLMSummarizingCondenserConfig(BaseModel):
"""Configuration for LLMCondenser."""
type: Literal['llm'] = 'llm'
type: Literal['llm'] = Field(default='llm')
llm_config: LLMConfig = Field(
..., description='Configuration for the LLM to use for condensing.'
)
@ -88,7 +88,7 @@ class LLMSummarizingCondenserConfig(BaseModel):
class AmortizedForgettingCondenserConfig(BaseModel):
"""Configuration for AmortizedForgettingCondenser."""
type: Literal['amortized'] = 'amortized'
type: Literal['amortized'] = Field(default='amortized')
max_size: int = Field(
default=100,
description='Maximum size of the condensed history before triggering forgetting.',
@ -108,7 +108,7 @@ class AmortizedForgettingCondenserConfig(BaseModel):
class LLMAttentionCondenserConfig(BaseModel):
"""Configuration for LLMAttentionCondenser."""
type: Literal['llm_attention'] = 'llm_attention'
type: Literal['llm_attention'] = Field(default='llm_attention')
llm_config: LLMConfig = Field(
..., description='Configuration for the LLM to use for attention.'
)
@ -131,7 +131,7 @@ class LLMAttentionCondenserConfig(BaseModel):
class StructuredSummaryCondenserConfig(BaseModel):
"""Configuration for StructuredSummaryCondenser instances."""
type: Literal['structured'] = 'structured'
type: Literal['structured'] = Field(default='structured')
llm_config: LLMConfig = Field(
..., description='Configuration for the LLM to use for condensing.'
)
@ -156,12 +156,9 @@ class StructuredSummaryCondenserConfig(BaseModel):
class CondenserPipelineConfig(BaseModel):
"""Configuration for the CondenserPipeline.
"""Configuration for the CondenserPipeline."""
Not currently supported by the TOML or ENV_VAR configuration strategies.
"""
type: Literal['pipeline'] = 'pipeline'
type: Literal['pipeline'] = Field(default='pipeline')
condensers: list[CondenserConfig] = Field(
default_factory=list,
description='List of condenser configurations to be used in the pipeline.',
@ -170,6 +167,17 @@ class CondenserPipelineConfig(BaseModel):
model_config = ConfigDict(extra='forbid')
class ConversationWindowCondenserConfig(BaseModel):
"""Configuration for ConversationWindowCondenser.
Not currently supported by the TOML or ENV_VAR configuration strategies.
"""
type: Literal['conversation_window'] = Field(default='conversation_window')
model_config = ConfigDict(extra='forbid')
# Type alias for convenience
CondenserConfig = (
NoOpCondenserConfig
@ -181,14 +189,14 @@ CondenserConfig = (
| LLMAttentionCondenserConfig
| StructuredSummaryCondenserConfig
| CondenserPipelineConfig
| ConversationWindowCondenserConfig
)
def condenser_config_from_toml_section(
data: dict, llm_configs: dict | None = None
) -> dict[str, CondenserConfig]:
"""
Create a CondenserConfig instance from a toml dictionary representing the [condenser] section.
"""Create a CondenserConfig instance from a toml dictionary representing the [condenser] section.
For CondenserConfig, the handling is different since it's a union type. The type of condenser
is determined by the 'type' field in the section.
@ -210,7 +218,6 @@ def condenser_config_from_toml_section(
Returns:
dict[str, CondenserConfig]: A mapping where the key "condenser" corresponds to the configuration.
"""
# Initialize the result mapping
condenser_mapping: dict[str, CondenserConfig] = {}
@ -261,8 +268,7 @@ from_toml_section = condenser_config_from_toml_section
def create_condenser_config(condenser_type: str, data: dict) -> CondenserConfig:
"""
Create a CondenserConfig instance based on the specified type.
"""Create a CondenserConfig instance based on the specified type.
Args:
condenser_type: The type of condenser to create.
@ -284,6 +290,9 @@ def create_condenser_config(condenser_type: str, data: dict) -> CondenserConfig:
'amortized': AmortizedForgettingCondenserConfig,
'llm_attention': LLMAttentionCondenserConfig,
'structured': StructuredSummaryCondenserConfig,
'pipeline': CondenserPipelineConfig,
'conversation_window': ConversationWindowCondenserConfig,
'browser_output_masking': BrowserOutputCondenserConfig,
}
if condenser_type not in condenser_classes:

View File

@ -91,3 +91,6 @@ class ActionType(str, Enum):
CONDENSATION = 'condensation'
"""Condenses a list of events into a summary."""
CONDENSATION_REQUEST = 'condensation_request'
"""Request for condensation of a list of events."""

View File

@ -195,3 +195,18 @@ class CondensationAction(Action):
if self.summary:
return f'Summary: {self.summary}'
return f'Condenser is dropping the events: {self.forgotten}.'
@dataclass
class CondensationRequestAction(Action):
"""This action is used to request a condensation of the conversation history.
Attributes:
action (str): The action type, namely ActionType.CONDENSATION_REQUEST.
"""
action: str = ActionType.CONDENSATION_REQUEST
@property
def message(self) -> str:
return 'Requesting a condensation of the conversation history.'

View File

@ -9,6 +9,7 @@ from openhands.events.action.agent import (
AgentThinkAction,
ChangeAgentStateAction,
CondensationAction,
CondensationRequestAction,
RecallAction,
)
from openhands.events.action.browse import BrowseInteractiveAction, BrowseURLAction
@ -43,6 +44,7 @@ actions = (
MessageAction,
SystemMessageAction,
CondensationAction,
CondensationRequestAction,
MCPAction,
)

View File

@ -4,6 +4,9 @@ from openhands.memory.condenser.impl.amortized_forgetting_condenser import (
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,
@ -34,4 +37,5 @@ __all__ = [
'RecentEventsCondenser',
'StructuredSummaryCondenser',
'CondenserPipeline',
'ConversationWindowCondenser',
]

View File

@ -0,0 +1,185 @@
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.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
) -> ConversationWindowCondenser:
return ConversationWindowCondenser()
ConversationWindowCondenser.register_config(ConversationWindowCondenserConfig)

View File

@ -5,7 +5,7 @@ from typing import overload
from pydantic import BaseModel
from openhands.core.logger import openhands_logger as logger
from openhands.events.action.agent import CondensationAction
from openhands.events.action.agent import CondensationAction, CondensationRequestAction
from openhands.events.event import Event
from openhands.events.observation.agent import AgentCondensationObservation
@ -17,6 +17,7 @@ class View(BaseModel):
"""
events: list[Event]
unhandled_condensation_request: bool = False
def __len__(self) -> int:
return len(self.events)
@ -52,6 +53,8 @@ class View(BaseModel):
forgotten_event_ids.update(event.forgotten)
# Make sure we also forget the condensation action itself
forgotten_event_ids.add(event.id)
if isinstance(event, CondensationRequestAction):
forgotten_event_ids.add(event.id)
kept_events = [event for event in events if event.id not in forgotten_event_ids]
@ -74,4 +77,17 @@ class View(BaseModel):
summary_offset, AgentCondensationObservation(content=summary)
)
return View(events=kept_events)
# Check for an unhandled condensation request -- these are events closer to the
# end of the list than any condensation action.
unhandled_condensation_request = False
for event in reversed(events):
if isinstance(event, CondensationAction):
break
if isinstance(event, CondensationRequestAction):
unhandled_condensation_request = True
break
return View(
events=kept_events,
unhandled_condensation_request=unhandled_condensation_request,
)

View File

@ -10,6 +10,7 @@ from openhands.core.config import OpenHandsConfig
from openhands.core.config.condenser_config import (
BrowserOutputCondenserConfig,
CondenserPipelineConfig,
ConversationWindowCondenserConfig,
LLMSummarizingCondenserConfig,
)
from openhands.core.config.mcp_config import MCPConfig, OpenHandsMCPConfigImpl
@ -156,13 +157,18 @@ class Session:
agent_config = self.config.get_agent_config(agent_cls)
if settings.enable_default_condenser:
# Default condenser chains a condenser that limits browser the total
# size of browser observations with a condenser that limits the size
# of the view given to the LLM. The order matters: with the browser
# output first, the summarizer will only see the most recent browser
# output, which should keep the summarization cost down.
# Default condenser chains three condensers together:
# 1. a conversation window condenser that handles explicit
# condensation requests,
# 2. a condenser that limits the total size of browser observations,
# and
# 3. a condenser that limits the size of the view given to the LLM.
# The order matters: with the browser output first, the summarizer
# will only see the most recent browser output, which should keep
# the summarization cost down.
default_condenser_config = CondenserPipelineConfig(
condensers=[
ConversationWindowCondenserConfig(),
BrowserOutputCondenserConfig(attention_window=2),
LLMSummarizingCondenserConfig(
llm_config=llm.config, keep_first=4, max_size=120

View File

@ -34,7 +34,12 @@ from openhands.events.observation.empty import NullObservation
from openhands.events.serialization import event_to_dict
from openhands.llm import LLM
from openhands.llm.metrics import Metrics, TokenUsage
from openhands.memory.condenser.condenser import Condensation
from openhands.memory.condenser.impl.conversation_window_condenser import (
ConversationWindowCondenser,
)
from openhands.memory.memory import Memory
from openhands.memory.view import View
from openhands.runtime.base import Runtime
from openhands.runtime.impl.action_execution.action_execution_client import (
ActionExecutionClient,
@ -835,8 +840,23 @@ async def test_notify_on_llm_retry(mock_agent, mock_event_stream, mock_status_ca
@pytest.mark.asyncio
@pytest.mark.parametrize(
'context_window_error',
[
ContextWindowExceededError(
message='prompt is too long: 233885 tokens > 200000 maximum',
model='',
llm_provider='',
),
BadRequestError(
message='litellm.BadRequestError: OpenrouterException - This endpoint\'s maximum context length is 40960 tokens. However, you requested about 42988 tokens (38892 of text input, 4096 in the output). Please reduce the length of either one, or use the "middle-out" transform to compress your prompt automatically.',
model='openrouter/qwen/qwen3-30b-a3b',
llm_provider='openrouter',
),
],
)
async def test_context_window_exceeded_error_handling(
mock_agent, mock_runtime, test_event_stream, mock_memory
context_window_error, mock_agent, mock_runtime, test_event_stream, mock_memory
):
"""Test that context window exceeded errors are handled correctly by the controller, providing a smaller view but keeping the history intact."""
max_iterations = 5
@ -847,9 +867,15 @@ async def test_context_window_exceeded_error_handling(
self.has_errored = False
self.index = 0
self.views = []
self.condenser = ConversationWindowCondenser()
def step(self, state: State):
self.views.append(state.view)
match self.condenser.condense(state.view):
case View() as view:
self.views.append(view)
case Condensation(action=action):
return action
# Wait until the right step to throw the error, and make sure we
# only throw it once.
@ -857,13 +883,13 @@ async def test_context_window_exceeded_error_handling(
self.index += 1
return MessageAction(content=f'Test message {self.index}')
error = ContextWindowExceededError(
ContextWindowExceededError(
message='prompt is too long: 233885 tokens > 200000 maximum',
model='',
llm_provider='',
)
self.has_errored = True
raise error
raise context_window_error
step_state = StepState()
mock_agent.step = step_state.step
@ -881,7 +907,7 @@ async def test_context_window_exceeded_error_handling(
content='Test microagent content',
recall_type=RecallType.KNOWLEDGE,
)
microagent_obs._cause = event.id
microagent_obs._cause = event.id # type: ignore
test_event_stream.add_event(microagent_obs, EventSource.ENVIRONMENT)
test_event_stream.subscribe(
@ -911,7 +937,7 @@ async def test_context_window_exceeded_error_handling(
# Check that the context window exception was thrown and the controller
# called the agent's `step` function the right number of times.
assert step_state.has_errored
assert len(step_state.views) == max_iterations
assert len(step_state.views) == max_iterations - 1
print('step_state.views: ', step_state.views)
# Look at pre/post-step views. Normally, these should always increase in
@ -936,7 +962,7 @@ async def test_context_window_exceeded_error_handling(
assert len(first_view) < len(second_view)
# The final state's history should contain:
# - max_iterations number of message actions,
# - (max_iterations - 1) number of message actions (one iteration taken up with the condensation request)
# - 1 recall actions,
# - 1 recall observations,
# - 1 condensation action.
@ -944,7 +970,7 @@ async def test_context_window_exceeded_error_handling(
len(
[event for event in final_state.history if isinstance(event, MessageAction)]
)
== max_iterations
== max_iterations - 1
)
assert (
len(
@ -955,7 +981,7 @@ async def test_context_window_exceeded_error_handling(
and event.source == EventSource.AGENT
]
)
== max_iterations - 1
== max_iterations - 2
)
assert (
len([event for event in final_state.history if isinstance(event, RecallAction)])
@ -1001,8 +1027,14 @@ async def test_run_controller_with_context_window_exceeded_with_truncation(
class StepState:
def __init__(self):
self.has_errored = False
self.condenser = ConversationWindowCondenser()
def step(self, state: State):
match self.condenser.condense(state.view):
case Condensation(action=action):
return action
case _:
pass
# If the state has more than one message and we haven't errored yet,
# throw the context window exceeded error
if len(state.history) > 5 and not self.has_errored:
@ -1614,206 +1646,3 @@ def test_system_message_in_event_stream(mock_agent, test_event_stream):
assert isinstance(events[0], SystemMessageAction)
assert events[0].content == 'Test system message'
assert events[0].tools == ['test_tool']
@pytest.mark.asyncio
async def test_openrouter_context_window_exceeded_error(
mock_agent, test_event_stream, mock_status_callback
):
"""Test that OpenRouter context window exceeded errors are properly detected and handled."""
max_iterations = 5
error_after = 2
class StepState:
def __init__(self):
self.has_errored = False
self.index = 0
self.views = []
def step(self, state: State):
self.views.append(state.view)
# Wait until the right step to throw the error, and make sure we
# only throw it once.
if self.index < error_after or self.has_errored:
self.index += 1
return MessageAction(content=f'Test message {self.index}')
# Create a BadRequestError with the OpenRouter context window exceeded message pattern
error = BadRequestError(
message='litellm.BadRequestError: OpenrouterException - This endpoint\'s maximum context length is 40960 tokens. However, you requested about 42988 tokens (38892 of text input, 4096 in the output). Please reduce the length of either one, or use the "middle-out" transform to compress your prompt automatically.',
model='openrouter/qwen/qwen3-30b-a3b',
llm_provider='openrouter',
)
self.has_errored = True
raise error
step_state = StepState()
mock_agent.step = step_state.step
mock_agent.config = AgentConfig(enable_history_truncation=True)
controller = AgentController(
agent=mock_agent,
event_stream=test_event_stream,
iteration_delta=max_iterations,
sid='test',
confirmation_mode=False,
headless_mode=True,
status_callback=mock_status_callback,
)
# Set the agent state to RUNNING
controller.state.agent_state = AgentState.RUNNING
# Run the controller until it hits the error
for _ in range(error_after + 2): # +2 to ensure we go past the error
await controller._step()
if step_state.has_errored:
break
# Verify that the error was handled as a context window exceeded error
# by checking that _handle_long_context_error was called (which adds a CondensationAction)
events = list(test_event_stream.get_events())
condensation_actions = [e for e in events if isinstance(e, CondensationAction)]
# There should be at least one CondensationAction if the error was handled correctly
assert len(condensation_actions) > 0, (
'OpenRouter context window exceeded error was not handled correctly'
)
await controller.close()
@pytest.mark.asyncio
async def test_sambanova_context_window_exceeded_error(
mock_agent, test_event_stream, mock_status_callback
):
"""Test that SambaNova context window exceeded errors are properly detected and handled."""
max_iterations = 5
error_after = 2
class StepState:
def __init__(self):
self.has_errored = False
self.index = 0
self.views = []
def step(self, state: State):
# Store the view for later inspection
self.views.append(state.view)
# only throw it once.
if self.index < error_after or self.has_errored:
self.index += 1
return MessageAction(content=f'Test message {self.index}')
# Create a BadRequestError with the SambaNova context window exceeded message pattern
error = BadRequestError(
message='litellm.BadRequestError: SambanovaException - The maximum context length of DeepSeek-V3-0324 is 32768. However, answering your request will take 39732 tokens. Please reduce the length of the messages or the specified max_completion_tokens value.',
model='sambanova/deepseek-v3-0324',
llm_provider='sambanova',
)
self.has_errored = True
raise error
step_state = StepState()
mock_agent.step = step_state.step
mock_agent.config = AgentConfig(enable_history_truncation=True)
controller = AgentController(
agent=mock_agent,
event_stream=test_event_stream,
iteration_delta=max_iterations,
sid='test',
confirmation_mode=False,
headless_mode=True,
status_callback=mock_status_callback,
)
# Set the agent state to RUNNING
controller.state.agent_state = AgentState.RUNNING
# Run the controller until it hits the error
for _ in range(error_after + 2): # +2 to ensure we go past the error
await controller._step()
if step_state.has_errored:
break
# Verify that the error was handled as a context window exceeded error
# by checking that _handle_long_context_error was called (which adds a CondensationAction)
events = list(test_event_stream.get_events())
condensation_actions = [e for e in events if isinstance(e, CondensationAction)]
# There should be at least one CondensationAction if the error was handled correctly
assert len(condensation_actions) > 0, (
'SambaNova context window exceeded error was not handled correctly'
)
await controller.close()
@pytest.mark.asyncio
async def test_sambanova_generic_exception_not_handled_as_context_error(
mock_agent, test_event_stream, mock_status_callback
):
"""Test that generic SambaNova exceptions (without context length pattern) are NOT handled as context window errors."""
max_iterations = 5
error_after = 2
class StepState:
def __init__(self):
self.has_errored = False
self.index = 0
self.views = []
def step(self, state: State):
# Store the view for later inspection
self.views.append(state.view)
# only throw it once.
if self.index < error_after or self.has_errored:
self.index += 1
return MessageAction(content=f'Test message {self.index}')
# Create a BadRequestError with a generic SambaNova error (no context length pattern)
error = BadRequestError(
message='litellm.BadRequestError: SambanovaException - Some other error occurred',
model='sambanova/deepseek-v3-0324',
llm_provider='sambanova',
)
self.has_errored = True
raise error
step_state = StepState()
mock_agent.step = step_state.step
mock_agent.config = AgentConfig(enable_history_truncation=True)
controller = AgentController(
agent=mock_agent,
event_stream=test_event_stream,
iteration_delta=max_iterations,
sid='test',
confirmation_mode=False,
headless_mode=True,
status_callback=mock_status_callback,
)
# Set the agent state to RUNNING
controller.state.agent_state = AgentState.RUNNING
# Run the controller until it hits the error
with pytest.raises(BadRequestError):
for _ in range(error_after + 2): # +2 to ensure we go past the error
await controller._step()
if step_state.has_errored:
break
# Verify that the error was NOT handled as a context window exceeded error
# by checking that _handle_long_context_error was NOT called (no CondensationAction should be added)
events = list(test_event_stream.get_events())
condensation_actions = [e for e in events if isinstance(e, CondensationAction)]
# There should be NO CondensationAction if the error was correctly NOT handled as context window error
assert len(condensation_actions) == 0, (
'Generic SambaNova exception was incorrectly handled as context window error'
)
await controller.close()

View File

@ -1,24 +1,34 @@
from unittest.mock import MagicMock, patch
"""
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.controller.agent import Agent
from openhands.controller.agent_controller import AgentController
from openhands.controller.state.state import State
from openhands.core.config import OpenHandsConfig
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.event import RecallType
from openhands.events.observation import (
CmdOutputObservation,
Observation,
RecallObservation,
)
from openhands.events.stream import EventStream
from openhands.llm.llm import LLM
from openhands.llm.metrics import Metrics
from openhands.storage.memory import InMemoryFileStore
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
@ -86,44 +96,20 @@ def create_events(event_data):
@pytest.fixture
def controller_fixture():
mock_agent = MagicMock(spec=Agent)
mock_agent.llm = MagicMock(spec=LLM)
mock_agent.llm.metrics = Metrics()
mock_agent.llm.config = OpenHandsConfig().get_llm_config()
mock_agent.config = OpenHandsConfig().get_agent_config('CodeActAgent')
mock_event_stream = MagicMock(spec=EventStream)
mock_event_stream.sid = 'test_sid'
mock_event_stream.file_store = InMemoryFileStore({})
# Ensure get_latest_event_id returns an integer
mock_event_stream.get_latest_event_id.return_value = -1
# Create a state with iteration_flag.max_value set to 10
state = State(inputs={}, session_id='test_sid')
state.iteration_flag.max_value = 10
controller = AgentController(
agent=mock_agent,
event_stream=mock_event_stream,
iteration_delta=1, # Add the required iteration_delta parameter
sid='test_sid',
initial_state=state,
)
# Don't mock _first_user_message anymore since we need it to work with history
return controller
def condenser_fixture():
condenser = ConversationWindowCondenser()
return condenser
# =============================================
# Test Cases for _apply_conversation_window
# Test Cases for ConversationWindowCondenser
# =============================================
def test_basic_truncation(controller_fixture):
controller = controller_fixture
def test_basic_truncation(condenser_fixture):
condenser = condenser_fixture
controller.state.history = create_events(
events = create_events(
[
{'type': SystemMessageAction, 'content': 'System Prompt'}, # 1
{
@ -156,6 +142,7 @@ def test_basic_truncation(controller_fixture):
}, # 10
]
)
view = View(events=events)
# Calculation (RecallAction now essential):
# History len = 10
@ -167,21 +154,22 @@ def test_basic_truncation(controller_fixture):
# 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.
truncated_events = controller._apply_conversation_window(controller.state.history)
# Forgotten IDs: [5, 6, 7, 8]
condensation = condenser.get_condensation(view)
assert len(truncated_events) == 6
expected_ids = [1, 2, 3, 4, 9, 10]
actual_ids = [e.id for e in truncated_events]
assert actual_ids == expected_ids
# Check no dangling observations at the start of the recent slice part
# The first event of the validated slice is cmd3(9)
assert not isinstance(truncated_events[4], Observation) # Index adjusted
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(controller_fixture):
controller = controller_fixture
def test_no_system_message(condenser_fixture):
condenser = condenser_fixture
controller.state.history = create_events(
events = create_events(
[
{
'type': MessageAction,
@ -213,7 +201,7 @@ def test_no_system_message(controller_fixture):
}, # 9
]
)
# No longer need to set mock ID
view = View(events=events)
# Calculation (RecallAction now essential):
# History len = 9
@ -224,19 +212,22 @@ def test_no_system_message(controller_fixture):
# 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 IDs: [1, 2, 3, 8, 9]. Length 5.
truncated_events = controller._apply_conversation_window(controller.state.history)
# Expected kept IDs: [1, 2, 3, 8, 9]. Length 5.
# Forgotten IDs: [4, 5, 6, 7]
condensation = condenser.get_condensation(view)
assert len(truncated_events) == 5
expected_ids = [1, 2, 3, 8, 9]
actual_ids = [e.id for e in truncated_events]
assert actual_ids == expected_ids
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(controller_fixture):
controller = controller_fixture
def test_no_recall_observation(condenser_fixture):
condenser = condenser_fixture
controller.state.history = create_events(
events = create_events(
[
{'type': SystemMessageAction, 'content': 'System Prompt'}, # 1
{
@ -269,29 +260,33 @@ def test_no_recall_observation(controller_fixture):
}, # 9
]
)
view = View(events=events)
# Calculation (RecallAction essential only if RecallObs exists):
# Calculation (RecallAction essential even without RecallObs in condenser):
# History len = 9
# Essentials = [sys(1), user(2)] (len=2) - RecallObs missing, so RecallAction not essential here
# Non-essential count = 9 - 2 = 7
# num_recent_to_keep = max(1, 7 // 2) = 3
# 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 IDs: [1, 2, 3, 8, 9]. Length 5.
truncated_events = controller._apply_conversation_window(controller.state.history)
# Expected kept IDs: [1, 2, 3, 8, 9]. Length 5.
# Forgotten IDs: [4, 5, 6, 7]
condensation = condenser.get_condensation(view)
assert len(truncated_events) == 5
expected_ids = [1, 2, 3, 8, 9]
actual_ids = [e.id for e in truncated_events]
assert actual_ids == expected_ids
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(controller_fixture):
controller = controller_fixture
def test_short_history_no_truncation(condenser_fixture):
condenser = condenser_fixture
history = create_events(
events = create_events(
[
{'type': SystemMessageAction, 'content': 'System Prompt'}, # 1
{
@ -310,7 +305,7 @@ def test_short_history_no_truncation(controller_fixture):
}, # 6
]
)
controller.state.history = history
view = View(events=events)
# Calculation (RecallAction now essential):
# History len = 6
@ -321,19 +316,22 @@ def test_short_history_no_truncation(controller_fixture):
# 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 IDs: [1, 2, 3, 4]. Length 4.
truncated_events = controller._apply_conversation_window(controller.state.history)
# Expected kept IDs: [1, 2, 3, 4]. Length 4.
# Forgotten IDs: [5, 6]
condensation = condenser.get_condensation(view)
assert len(truncated_events) == 4
expected_ids = [1, 2, 3, 4]
actual_ids = [e.id for e in truncated_events]
assert actual_ids == expected_ids
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(controller_fixture):
controller = controller_fixture
def test_only_essential_events(condenser_fixture):
condenser = condenser_fixture
history = create_events(
events = create_events(
[
{'type': SystemMessageAction, 'content': 'System Prompt'}, # 1
{
@ -345,7 +343,7 @@ def test_only_essential_events(controller_fixture):
{'type': RecallObservation, 'content': 'Recall result', 'cause_id': 3}, # 4
]
)
controller.state.history = history
view = View(events=events)
# Calculation (RecallAction now essential):
# History len = 4
@ -356,19 +354,22 @@ def test_only_essential_events(controller_fixture):
# 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 IDs: [1, 2, 3, 4]. Length 4.
truncated_events = controller._apply_conversation_window(controller.state.history)
# Expected kept IDs: [1, 2, 3, 4]. Length 4.
# Forgotten IDs: []
condensation = condenser.get_condensation(view)
assert len(truncated_events) == 4
expected_ids = [1, 2, 3, 4]
actual_ids = [e.id for e in truncated_events]
assert actual_ids == expected_ids
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(controller_fixture):
controller = controller_fixture
def test_dangling_observations_at_cut_point(condenser_fixture):
condenser = condenser_fixture
history_forced_dangle = create_events(
events = create_events(
[
{'type': SystemMessageAction, 'content': 'System Prompt'}, # 1
{
@ -405,7 +406,7 @@ def test_dangling_observations_at_cut_point(controller_fixture):
}, # 10
]
) # 10 events total
controller.state.history = history_forced_dangle
view = View(events=events)
# Calculation (RecallAction now essential):
# History len = 10
@ -416,20 +417,22 @@ def test_dangling_observations_at_cut_point(controller_fixture):
# 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 IDs: [1, 2, 3, 4, 9, 10]. Length 6.
truncated_events = controller._apply_conversation_window(controller.state.history)
# Expected kept IDs: [1, 2, 3, 4, 9, 10]. Length 6.
# Forgotten IDs: [5, 6, 7, 8]
condensation = condenser.get_condensation(view)
assert len(truncated_events) == 6
expected_ids = [1, 2, 3, 4, 9, 10]
actual_ids = [e.id for e in truncated_events]
assert actual_ids == expected_ids
# Verify dangling observations 5 and 6 were removed (implicitly by slice start and validation)
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(controller_fixture):
controller = controller_fixture
def test_only_dangling_observations_in_recent_slice(condenser_fixture):
condenser = condenser_fixture
history = create_events(
events = create_events(
[
{'type': SystemMessageAction, 'content': 'System Prompt'}, # 1
{
@ -452,7 +455,7 @@ def test_only_dangling_observations_in_recent_slice(controller_fixture):
}, # 6 (Dangling)
]
) # 6 events total
controller.state.history = history
view = View(events=events)
# Calculation (RecallAction now essential):
# History len = 6
@ -463,43 +466,44 @@ def test_only_dangling_observations_in_recent_slice(controller_fixture):
# 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 IDs: [1, 2, 3, 4]. Length 4.
# Expected kept IDs: [1, 2, 3, 4]. Length 4.
# Forgotten IDs: [5, 6]
with patch(
'openhands.controller.agent_controller.logger.warning'
'openhands.memory.condenser.impl.conversation_window_condenser.logger.warning'
) as mock_log_warning:
truncated_events = controller._apply_conversation_window(
controller.state.history
)
condensation = condenser.get_condensation(view)
assert len(truncated_events) == 4
expected_ids = [1, 2, 3, 4]
actual_ids = [e.id for e in truncated_events]
assert actual_ids == expected_ids
# Verify dangling observations 5 and 6 were removed
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, allowing for variations like stacklevel
# 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]
assert 'extra' in call_kwargs
assert call_kwargs['extra'].get('session_id') == 'test_sid'
def test_empty_history(controller_fixture):
controller = controller_fixture
controller.state.history = []
def test_empty_history(condenser_fixture):
condenser = condenser_fixture
view = View(events=[])
truncated_events = controller._apply_conversation_window(controller.state.history)
assert truncated_events == []
condensation = condenser.get_condensation(view)
assert isinstance(condensation, Condensation)
assert isinstance(condensation.action, CondensationAction)
assert condensation.action.forgotten == []
def test_multiple_user_messages(controller_fixture):
controller = controller_fixture
def test_multiple_user_messages(condenser_fixture):
condenser = condenser_fixture
history = create_events(
events = create_events(
[
{'type': SystemMessageAction, 'content': 'System Prompt'}, # 1
{
@ -540,7 +544,7 @@ def test_multiple_user_messages(controller_fixture):
}, # 11
]
) # 11 events total
controller.state.history = history
view = View(events=events)
# Calculation (RecallAction now essential):
# History len = 11
@ -551,15 +555,18 @@ def test_multiple_user_messages(controller_fixture):
# 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 IDs: [1, 2, 3, 4, 10, 11]. Length 6.
truncated_events = controller._apply_conversation_window(controller.state.history)
# Expected kept IDs: [1, 2, 3, 4, 10, 11]. Length 6.
# Forgotten IDs: [5, 6, 7, 8, 9]
condensation = condenser.get_condensation(view)
assert len(truncated_events) == 6
expected_ids = [1, 2, 3, 4, 10, 11]
actual_ids = [e.id for e in truncated_events]
assert actual_ids == expected_ids
assert isinstance(condensation, Condensation)
assert isinstance(condensation.action, CondensationAction)
# Verify the second user message (ID 7) was NOT kept
assert not any(event.id == 7 for event in truncated_events)
# Verify the first user message (ID 2) is present
assert any(event.id == 2 for event in truncated_events)
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

View File

@ -1,4 +1,4 @@
from openhands.events.action.agent import CondensationAction
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
@ -98,6 +98,169 @@ def test_no_condensation_action_in_view() -> None:
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):