Repository navigation
fix: prevent O(n²) memory growth in ACP tool call streaming #3033
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
4edc9b1
fix: deduplicate ACP tool call events and truncate content to prevent…
simonrosenberg 7ea2d53
fix: deduplicate ACP tool call events in RemoteEventsList to prevent …
simonrosenberg 2843779
style: apply ruff formatting to acp_agent.py
simonrosenberg a0b8f04
style: apply ruff formatting to remote_conversation.py
simonrosenberg e706860
review: descriptive variable names, stale-index cleanup, and regressi…
39b1c30
style: fix ruff E501 line-too-long in warning message and docstrings
c4e8090
Merge branch 'main' into fix/acp-tool-call-dedup-and-truncation
simonrosenberg 0d469a0
Merge branch 'main' into fix/acp-tool-call-dedup-and-truncation
simonrosenberg File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
189 changes: 189 additions & 0 deletions
189
openhands-sdk/openhands/sdk/tests/test_acp_dedup_and_truncation.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,189 @@ | ||
| """Regression tests for ACP tool call deduplication and content truncation. | ||
|
|
||
| Covers: | ||
| - RemoteEventsList._add_event_unsafe deduplicates ACPToolCallEvent by tool_call_id | ||
| - _serialize_tool_content truncates text blocks to MAX_ACP_CONTENT_CHARS | ||
| - _emit_tool_call_event (via _serialize_tool_content) preserves non-text blocks | ||
| - Stale index entry is cleaned up and a warning is logged | ||
| """ | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| import logging | ||
| import threading | ||
| import unittest | ||
| from unittest.mock import MagicMock, patch | ||
|
|
||
| from openhands.sdk.agent.acp_agent import MAX_ACP_CONTENT_CHARS, _serialize_tool_content | ||
| from openhands.sdk.conversation.impl.remote_conversation import RemoteEventsList | ||
| from openhands.sdk.event.acp_tool_call import ACPToolCallEvent | ||
|
|
||
|
|
||
| def _make_tool_call_event(tool_call_id: str, raw_output: str = "") -> ACPToolCallEvent: | ||
| return ACPToolCallEvent( | ||
| tool_call_id=tool_call_id, | ||
| title="test tool", | ||
| raw_output=raw_output, | ||
| ) | ||
|
|
||
|
|
||
| def _make_events_list() -> RemoteEventsList: | ||
| """Return a RemoteEventsList with _do_full_sync stubbed out.""" | ||
| with patch.object(RemoteEventsList, "_do_full_sync"): | ||
| client = MagicMock() | ||
| return RemoteEventsList(client=client, conversation_id="conv-1") | ||
|
|
||
|
|
||
| class TestACPToolCallDeduplication(unittest.TestCase): | ||
| def setUp(self) -> None: | ||
| self.events = _make_events_list() | ||
|
|
||
| def _add(self, event: ACPToolCallEvent) -> None: | ||
| with self.events._lock: | ||
| self.events._add_event_unsafe(event) | ||
|
|
||
| def test_first_event_is_added(self) -> None: | ||
| ev = _make_tool_call_event("tc-1", "output-1") | ||
| self._add(ev) | ||
| self.assertEqual(len(self.events._cached_events), 1) | ||
| self.assertIn(ev.id, self.events._cached_event_ids) | ||
|
|
||
| def test_subsequent_events_replace_not_append(self) -> None: | ||
| ev1 = _make_tool_call_event("tc-1", "output-1") | ||
| ev2 = _make_tool_call_event("tc-1", "output-1-updated") | ||
| ev3 = _make_tool_call_event("tc-1", "output-1-final") | ||
| self._add(ev1) | ||
| self._add(ev2) | ||
| self._add(ev3) | ||
|
|
||
| self.assertEqual(len(self.events._cached_events), 1) | ||
| last = self.events._cached_events[0] | ||
| assert isinstance(last, ACPToolCallEvent) | ||
| self.assertEqual(last.raw_output, "output-1-final") | ||
| self.assertNotIn(ev1.id, self.events._cached_event_ids) | ||
| self.assertNotIn(ev2.id, self.events._cached_event_ids) | ||
| self.assertIn(ev3.id, self.events._cached_event_ids) | ||
|
|
||
| def test_different_tool_call_ids_are_kept_separately(self) -> None: | ||
| ev_a = _make_tool_call_event("tc-a", "a-output") | ||
| ev_b = _make_tool_call_event("tc-b", "b-output") | ||
| self._add(ev_a) | ||
| self._add(ev_b) | ||
|
|
||
| self.assertEqual(len(self.events._cached_events), 2) | ||
| ids = { | ||
| e.tool_call_id | ||
| for e in self.events._cached_events | ||
| if isinstance(e, ACPToolCallEvent) | ||
| } | ||
| self.assertEqual(ids, {"tc-a", "tc-b"}) | ||
|
|
||
| def test_index_stays_consistent_after_replacement(self) -> None: | ||
| ev1 = _make_tool_call_event("tc-1", "v1") | ||
| ev2 = _make_tool_call_event("tc-1", "v2") | ||
| self._add(ev1) | ||
| self._add(ev2) | ||
|
|
||
| self.assertEqual(self.events._acp_tool_call_id_to_event_id["tc-1"], ev2.id) | ||
|
|
||
| def test_stale_index_entry_is_cleaned_up_with_warning(self) -> None: | ||
| ev1 = _make_tool_call_event("tc-1", "v1") | ||
| self._add(ev1) | ||
|
|
||
| # Manually corrupt state: remove ev1 from _cached_events but leave index intact | ||
| self.events._cached_events.clear() | ||
| self.events._cached_event_ids.discard(ev1.id) | ||
|
|
||
| ev2 = _make_tool_call_event("tc-1", "v2") | ||
| with self.assertLogs("openhands.sdk", level=logging.WARNING) as log_ctx: | ||
| self._add(ev2) | ||
|
|
||
| self.assertTrue( | ||
| any("Stale" in line for line in log_ctx.output), | ||
| "Expected a stale-index warning to be logged", | ||
| ) | ||
| # ev2 should be inserted normally after cleanup | ||
| self.assertEqual(len(self.events._cached_events), 1) | ||
| self.assertEqual(self.events._cached_events[0].id, ev2.id) | ||
| self.assertEqual(self.events._acp_tool_call_id_to_event_id["tc-1"], ev2.id) | ||
|
|
||
| def test_thread_safety_concurrent_updates(self) -> None: | ||
| """Concurrent updates to the same tool_call_id must not corrupt state.""" | ||
| errors: list[Exception] = [] | ||
|
|
||
| def updater(i: int) -> None: | ||
| try: | ||
| ev = _make_tool_call_event("tc-shared", f"output-{i}") | ||
| self._add(ev) | ||
| except Exception as exc: | ||
| errors.append(exc) | ||
|
|
||
| threads = [threading.Thread(target=updater, args=(i,)) for i in range(20)] | ||
| for t in threads: | ||
| t.start() | ||
| for t in threads: | ||
| t.join() | ||
|
|
||
| self.assertEqual(errors, []) | ||
| # Only one event per tool_call_id should survive | ||
| tc_events = [ | ||
| e | ||
| for e in self.events._cached_events | ||
| if isinstance(e, ACPToolCallEvent) and e.tool_call_id == "tc-shared" | ||
| ] | ||
| self.assertEqual(len(tc_events), 1) | ||
|
|
||
|
|
||
| class TestSerializeToolContentTruncation(unittest.TestCase): | ||
| def test_short_text_is_not_truncated(self) -> None: | ||
| content = [{"type": "text", "text": "short"}] | ||
| result = _serialize_tool_content(content) | ||
| assert result is not None | ||
| self.assertEqual(result[0]["text"], "short") | ||
|
|
||
| def test_long_text_is_truncated_to_max(self) -> None: | ||
| long_text = "x" * (MAX_ACP_CONTENT_CHARS + 5_000) | ||
| content = [{"type": "text", "text": long_text}] | ||
| result = _serialize_tool_content(content) | ||
| assert result is not None | ||
| self.assertLessEqual(len(result[0]["text"]), MAX_ACP_CONTENT_CHARS + 200) | ||
|
|
||
| def test_non_text_blocks_are_not_modified(self) -> None: | ||
| big_data = "y" * (MAX_ACP_CONTENT_CHARS + 1_000) | ||
| content = [{"type": "image_url", "url": big_data}] | ||
| result = _serialize_tool_content(content) | ||
| assert result is not None | ||
| self.assertEqual(result[0]["url"], big_data) | ||
|
|
||
| def test_none_content_returns_none(self) -> None: | ||
| self.assertIsNone(_serialize_tool_content(None)) | ||
|
|
||
| def test_empty_content_returns_none(self) -> None: | ||
| self.assertIsNone(_serialize_tool_content([])) | ||
|
|
||
| def test_mixed_blocks_only_truncates_text(self) -> None: | ||
| long_text = "a" * (MAX_ACP_CONTENT_CHARS + 1_000) | ||
| big_url = "b" * (MAX_ACP_CONTENT_CHARS + 1_000) | ||
| content = [ | ||
| {"type": "text", "text": long_text}, | ||
| {"type": "image_url", "url": big_url}, | ||
| ] | ||
| result = _serialize_tool_content(content) | ||
| assert result is not None | ||
| self.assertLessEqual(len(result[0]["text"]), MAX_ACP_CONTENT_CHARS + 200) | ||
| self.assertEqual(len(result[1]["url"]), MAX_ACP_CONTENT_CHARS + 1_000) | ||
|
|
||
| def test_pydantic_model_content_is_serialized(self) -> None: | ||
| """Blocks with model_dump() are serialized before the truncation check.""" | ||
|
|
||
| class FakeBlock: | ||
| def model_dump(self, **_kwargs: object) -> dict: | ||
| return {"type": "text", "text": "hello"} | ||
|
|
||
| result = _serialize_tool_content([FakeBlock()]) | ||
| assert result is not None | ||
| self.assertEqual(result[0]["text"], "hello") | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| unittest.main() |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.