Skip to content
120 changes: 58 additions & 62 deletions openhands-sdk/openhands/sdk/context/view/view.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
from __future__ import annotations

from collections.abc import Sequence
from functools import cached_property
from logging import getLogger
from typing import overload

Expand All @@ -28,18 +27,15 @@ class View(BaseModel):
in deciding whether further condensation is needed.
"""

events: list[LLMConvertibleEvent]
events: list[LLMConvertibleEvent] = Field(default_factory=list)

unhandled_condensation_request: bool = False
"""Whether there is an unhandled condensation request in the view."""

condensations: list[Condensation] = Field(default_factory=list)
"""A list of condensations that were processed to produce the view."""

def __len__(self) -> int:
return len(self.events)

@cached_property
@property
Comment thread
csmith49 marked this conversation as resolved.
Resolved
def manipulation_indices(self) -> ManipulationIndices:
"""The indices where the view events can be manipulated without violating the
properties expected by LLM APIs.
Expand Down Expand Up @@ -75,26 +71,10 @@ def __getitem__(
else:
raise ValueError(f"Invalid key type: {type(key)}")

@staticmethod
def unhandled_condensation_request_exists(
events: Sequence[Event],
) -> bool:
"""Check if there is an unhandled condensation request in the list of events.

An unhandled condensation request is defined as a CondensationRequest event
that appears after the most recent Condensation event in the list.
"""
for event in reversed(events):
if isinstance(event, Condensation):
return False
if isinstance(event, CondensationRequest):
return True
return False

@staticmethod
def enforce_properties(
current_view_events: list[LLMConvertibleEvent], all_events: Sequence[Event]
) -> list[LLMConvertibleEvent]:
self,
all_events: Sequence[Event],
) -> None:
"""Enforce all properties on the list of current view events.

Repeatedly applies each property's enforcement mechanism until the list of view
Expand All @@ -103,62 +83,78 @@ def enforce_properties(
Since enforcement is intended as a fallback to inductively maintaining the
properties via the associated manipulation indices, any time a property must be
enforced a warning is logged.

Modifies the view in-place.
"""
for property in ALL_PROPERTIES:
events_to_forget = property.enforce(current_view_events, all_events)
events_to_forget = property.enforce(self.events, all_events)
if events_to_forget:
logger.warning(
f"Property {property.__class__} enforced, "
f"{len(events_to_forget)} events dropped."
)
return View.enforce_properties(
[
event
for event in current_view_events
if event.id not in events_to_forget
],
all_events,
)
return current_view_events

@staticmethod
def from_events(events: Sequence[Event]) -> View:
"""Create a view from a list of events, respecting the semantics of any
condensation events.
"""
output: list[LLMConvertibleEvent] = []
condensations: list[Condensation] = []
self.events = [
event for event in self.events if event.id not in events_to_forget
]
Comment thread
csmith49 marked this conversation as resolved.
Resolved
break

# Generate the LLMConvertibleEvent objects the agent can send to the LLM by
# removing un-sendable events and applying condensations in order.
for event in events:
# By the time we come across a Condensation event, the output list should
# If we get all the way through the loop without hitting a break, that means no
# properties needed to be enforced and we can keep the view as-is.
else:
return

# If we did hit a break in the loop, a property applied and now we need to check
# all the properties again to see if any are unblocked.
self.enforce_properties(all_events)

def append_event(self, event: Event) -> None:
"""Append an event to the end of the view, applying any condensation semantics
as we do.

Modifies the view in-place.
"""
match event:
# By the time we come across a Condensation event, the event list should
# already reflect the events seen by the agent up to that point. We can
# therefore apply the condensation semantics directly to the output list.
if isinstance(event, Condensation):
condensations.append(event)
output = event.apply(output)
# therefore apply the condensation semantics directly to the stored events.
case Condensation():
self.events = event.apply(self.events)
self.unhandled_condensation_request = False

case CondensationRequest():
self.unhandled_condensation_request = True

elif isinstance(event, LLMConvertibleEvent):
output.append(event)
case LLMConvertibleEvent():
self.events.append(event)

Comment thread
csmith49 marked this conversation as resolved.
Resolved
# If the event isn't related to condensation and isn't LLMConvertible, it
# should not be in the resulting view. Examples include certain internal
# events used for state tracking that the LLM does not need to see -- see,
# for example, ConversationStateUpdateEvent, PauseEvent, and (relevant here)
# CondensationRequest.
else:
case _:
logger.debug(
f"Skipping non-LLMConvertibleEvent of type {type(event)} "
"in View.from_events"
"in View.append_event"
)

output = View.enforce_properties(output, events)
@staticmethod
def from_events(events: Sequence[Event]) -> View:
"""Create a view from a list of events, respecting the semantics of any
condensation events.
"""
result: View = View()

# Generate the LLMConvertibleEvent objects the agent can send to the LLM by
# adding them one at a time to the result view. This ensures condensations are
# applied in the order they were generated and condensation requests are
# appropriately tracked.
for event in events:
result.append_event(event)

# Once all the events are loaded enforce the relevant properties to ensure
# the construction was done properly.
result.enforce_properties(events)
Comment thread
csmith49 marked this conversation as resolved.
Resolved

return View(
events=output,
unhandled_condensation_request=View.unhandled_condensation_request_exists(
events
),
condensations=condensations,
)
return result
82 changes: 0 additions & 82 deletions tests/sdk/context/view/test_view.py
Original file line number Diff line number Diff line change
Expand Up @@ -292,85 +292,3 @@ def test_condensation_request_always_removed_from_view() -> None:
for event in view_handled:
assert not isinstance(event, CondensationRequest)
assert not isinstance(event, Condensation)


def test_condensations_field_empty_when_no_condensations() -> None:
"""Test that condensations field is empty when there are no condensation events."""
events: list[Event] = [message_event(f"Event {i}") for i in range(3)]
view = View.from_events(events)

assert view.condensations == []


def test_condensations_field_stores_all_condensations_in_order() -> None:
"""Test that condensations field stores all condensation events in chronological
order.
"""
message_events = [message_event(f"Event {i}") for i in range(5)]

# Create multiple condensations
condensation1 = Condensation(
forgotten_event_ids=[message_events[0].id],
summary="Summary 1",
llm_response_id="condensation_response_1",
)
condensation2 = Condensation(
forgotten_event_ids=[message_events[1].id],
summary="Summary 2",
llm_response_id="condensation_response_2",
)
condensation3 = Condensation(
forgotten_event_ids=[],
summary="Summary 3",
llm_response_id="condensation_response_3",
)

events: list[Event] = [
message_events[0],
message_events[1],
condensation1,
message_events[2],
condensation2,
message_events[3],
message_events[4],
condensation3,
]

view = View.from_events(events)

# Check that all condensations are stored in order
assert len(view.condensations) == 3
assert view.condensations[0] == condensation1
assert view.condensations[1] == condensation2
assert view.condensations[2] == condensation3


def test_condensations_field_with_mixed_events() -> None:
"""Test condensations field behavior with mixed event types including requests."""
message_events = [message_event(f"Event {i}") for i in range(4)]

condensation1 = Condensation(
forgotten_event_ids=[message_events[0].id],
llm_response_id="condensation_response_1",
)
condensation2 = Condensation(
forgotten_event_ids=[], llm_response_id="condensation_response_2"
)

events: list[Event] = [
message_events[0],
CondensationRequest(), # Should not appear in condensations
message_events[1],
condensation1,
message_events[2],
CondensationRequest(), # Should not appear in condensations
condensation2,
message_events[3],
]

view = View.from_events(events)

# Only actual Condensation events should be in the condensations field
assert len(view.condensations) == 2
assert view.condensations[0] == condensation1
assert view.condensations[1] == condensation2
Loading
Loading