Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 29 additions & 3 deletions openhands-agent-server/openhands/agent_server/event_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,10 @@
StreamingDeltaEvent,
)
from openhands.sdk.event.conversation_error import ConversationErrorEvent
from openhands.sdk.event.conversation_state import ConversationStateUpdateEvent
from openhands.sdk.event.conversation_state import (
FULL_STATE_KEY,
ConversationStateUpdateEvent,
)
from openhands.sdk.event.error_classification import ErrorClassification, FailureKind
from openhands.sdk.event.llm_completion_log import LLMCompletionLogEvent
from openhands.sdk.git.exceptions import GitCommandError, GitRepositoryError
Expand Down Expand Up @@ -111,6 +114,23 @@
_GIT_INIT_LOCK = threading.Lock()


def _without_skill_bodies(
event: ConversationStateUpdateEvent,
) -> ConversationStateUpdateEvent:
"""Leave skill bodies, most of the agent's size, out of the agent that
sockets and webhooks receive, as conversation responses do by default."""
if event.key == FULL_STATE_KEY:
agent = event.value.get("agent") or {}
elif event.key == "agent":
agent = event.value.model_dump(mode="json")
event = event.model_copy(update={"value": agent})
else:
return event
if agent.get("agent_context"):
agent["agent_context"]["skills"] = []
return event


class ConversationRunLimitExceeded(RateLimited, RuntimeError):
"""The server has no capacity to start another conversation run."""

Expand Down Expand Up @@ -847,7 +867,8 @@ def _create_state_update_event_sync(self) -> ConversationStateUpdateEvent:
raise ValueError("inactive_service")
state = self._conversation._state
with state:
return ConversationStateUpdateEvent.from_conversation_state(state)
event = ConversationStateUpdateEvent.from_conversation_state(state)
return _without_skill_bodies(event)

def _create_initial_state_sync(
self,
Expand Down Expand Up @@ -1325,7 +1346,7 @@ async def start(self):

# Create and store callback wrapper to allow flushing pending events
self._callback_wrapper = AsyncCallbackWrapper(
self._pub_sub, loop=asyncio.get_running_loop()
self._publish_to_subscribers, loop=asyncio.get_running_loop()
)

# Token streaming is wired only for agents that can actually emit token
Expand Down Expand Up @@ -2492,6 +2513,11 @@ async def get_state(self) -> ConversationState:
raise ValueError("inactive_service")
return self._conversation._state

async def _publish_to_subscribers(self, event: Event) -> None:
if isinstance(event, ConversationStateUpdateEvent):
event = _without_skill_bodies(event)
await self._pub_sub(event)

async def _publish_state_update(self):
"""Publish a ConversationStateUpdateEvent with the current state."""
if not self._conversation:
Expand Down
60 changes: 60 additions & 0 deletions tests/agent_server/test_event_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
from openhands.agent_server.pub_sub import Subscriber
from openhands.sdk import LLM, Agent, AgentBase, Conversation, Message
from openhands.sdk.agent import ACPAgent
from openhands.sdk.context import AgentContext
from openhands.sdk.context.condenser import NoOpCondenser
from openhands.sdk.conversation.event_store import EventLog
from openhands.sdk.conversation.exceptions import ConversationRunError
Expand Down Expand Up @@ -70,6 +71,7 @@
from openhands.sdk.mcp.config import coerce_mcp_config
from openhands.sdk.secret import StaticSecret
from openhands.sdk.security.confirmation_policy import NeverConfirm
from openhands.sdk.skills import Skill
from openhands.sdk.subagent.schema import AgentDefinition
from openhands.sdk.utils.cipher import FERNET_TOKEN_PREFIX, Cipher
from openhands.sdk.utils.pydantic_secrets import REDACTED_SECRET_VALUE
Expand Down Expand Up @@ -2808,6 +2810,64 @@ def mock_conversation_with_real_lock(self):
conversation._state = state
return conversation

@pytest.mark.asyncio
async def test_initial_state_leaves_out_skill_bodies(
self, event_service, mock_conversation_with_real_lock
):
state = mock_conversation_with_real_lock._state
state.model_dump.return_value = {
"execution_status": "idle",
"agent": {
"llm": {"model": "gpt-4o-mini"},
"agent_context": {
"skills": [{"name": "big", "content": "x" * 100_000}],
"system_message_suffix": "kept",
},
},
}
event_service._conversation = mock_conversation_with_real_lock
received: list[Event] = []

class Recorder(Subscriber[Event]):
async def __call__(self, event: Event):
received.append(event)

await event_service.subscribe_to_events(Recorder())

snapshot = cast(ConversationStateUpdateEvent, received[0])
assert snapshot.key == "full_state"
assert snapshot.value["agent"]["agent_context"] == {
"skills": [],
"system_message_suffix": "kept",
}
assert snapshot.value["agent"]["llm"] == {"model": "gpt-4o-mini"}
assert snapshot.value["execution_status"] == "idle"

@pytest.mark.asyncio
async def test_agent_update_leaves_out_skill_bodies(self, event_service):
agent = _sample_agent().model_copy(
update={
"agent_context": AgentContext(
skills=[Skill(name="big", content="x" * 100_000)],
system_message_suffix="kept",
)
}
)
received: list[Event] = []

class Recorder(Subscriber[Event]):
async def __call__(self, event: Event):
received.append(event)

event_service._pub_sub.subscribe(Recorder())
update = ConversationStateUpdateEvent(key="agent", value=agent)
await event_service._publish_to_subscribers(update)

sent = cast(ConversationStateUpdateEvent, received[0])
assert sent.value["agent_context"]["skills"] == []
assert sent.value["agent_context"]["system_message_suffix"] == "kept"
assert len(update.value.agent_context.skills) == 1

@pytest.mark.asyncio
async def test_concurrent_subscriptions_no_deadlock(
self, event_service, mock_conversation_with_real_lock
Expand Down
11 changes: 5 additions & 6 deletions tests/cross/test_remote_conversation_live_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,16 +86,15 @@ def _assert_secret(value: "str | SecretStr", expected: str) -> None:
assert value == expected


@pytest.mark.parametrize("skill_mib", [2, 17])
def test_attach_receives_large_initial_state(tmp_path: Path, monkeypatch, skill_mib):
@pytest.mark.parametrize("context_mib", [2, 17])
def test_attach_receives_large_initial_state(tmp_path: Path, monkeypatch, context_mib):
monkeypatch.setenv("OPENHANDS_REMOTE_WS_READY_TIMEOUT", "5")
# Skill bodies are left out of the snapshot, so grow a field that is sent.
agent = Agent(
llm=LLM(model="gpt-4o-mini", api_key=SecretStr("test")),
tools=[],
agent_context=AgentContext(
skills=[
Skill(name="large-context", content="x" * (skill_mib * 1024 * 1024))
]
system_message_suffix="x" * (context_mib * 1024 * 1024)
),
)
received = []
Expand All @@ -122,7 +121,7 @@ def on_event(event):
response.raise_for_status()
conversation_id = UUID(response.json()["id"])
try:
if skill_mib == 17:
if context_mib == 17:
with pytest.raises(
WebSocketConnectionError, match="message too big.*1009"
):
Expand Down
Loading