Repository navigation
Fix Laminar span stack warning in LocalConversation #1039
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
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
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
146 changes: 146 additions & 0 deletions
146
tests/sdk/conversation/local/test_span_double_ending.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,146 @@ | ||
| """Test for the span double-ending issue in LocalConversation.""" | ||
|
|
||
| import logging | ||
| import tempfile | ||
| from unittest.mock import patch | ||
|
|
||
| import pytest | ||
| from pydantic import SecretStr | ||
|
|
||
| from openhands.sdk.agent import Agent | ||
| from openhands.sdk.conversation.impl.local_conversation import LocalConversation | ||
| from openhands.sdk.llm import LLM | ||
|
|
||
|
|
||
| def create_test_agent() -> Agent: | ||
| """Create a test agent.""" | ||
| llm = LLM(model="gpt-4o-mini", api_key=SecretStr("test-key"), usage_id="test-llm") | ||
| return Agent(llm=llm, tools=[]) | ||
|
|
||
|
|
||
| def test_no_double_span_ending_warning(caplog): | ||
| """Test that LocalConversation doesn't produce double span ending warnings.""" | ||
|
|
||
| # Create test agent | ||
| agent = create_test_agent() | ||
|
|
||
| # Create a temporary workspace | ||
| with tempfile.TemporaryDirectory() as temp_dir: | ||
| # Create conversation | ||
| conversation = LocalConversation( | ||
| agent=agent, | ||
| workspace=temp_dir, | ||
| visualize=False, # Disable visualization to simplify test | ||
| ) | ||
|
|
||
| # Capture logs at WARNING level | ||
| with caplog.at_level(logging.WARNING): | ||
| # Mock the agent.step to raise an exception to trigger the finally block | ||
| with patch( | ||
| "openhands.sdk.agent.agent.Agent.step", | ||
| side_effect=Exception("Test exception"), | ||
| ): | ||
| # Try to run the conversation (will fail due to mocked exception) | ||
| with pytest.raises(Exception): | ||
| conversation.run() | ||
|
|
||
| # Close the conversation (this would normally be called by __del__) | ||
| conversation.close() | ||
|
|
||
| # Check that no warning about empty span stack was logged | ||
| warning_messages = [ | ||
| record.message for record in caplog.records if record.levelname == "WARNING" | ||
| ] | ||
| span_warnings = [ | ||
| msg | ||
| for msg in warning_messages | ||
| if "Attempted to end active span, but stack is empty" in msg | ||
| ] | ||
|
|
||
| # This test should fail initially (showing the bug exists) | ||
| # After the fix, there should be no span warnings | ||
| assert len(span_warnings) == 0, f"Found span warnings: {span_warnings}" | ||
|
|
||
|
|
||
| def test_span_ending_with_successful_run(caplog): | ||
| """Test span ending behavior with a successful run (no exceptions).""" | ||
|
|
||
| # Create test agent | ||
| agent = create_test_agent() | ||
|
|
||
| # Create a temporary workspace | ||
| with tempfile.TemporaryDirectory() as temp_dir: | ||
| # Create conversation | ||
| conversation = LocalConversation( | ||
| agent=agent, workspace=temp_dir, visualize=False | ||
| ) | ||
|
|
||
| # Mock the agent.step to finish immediately (no iterations) | ||
| def finish_immediately(*args, **kwargs): | ||
| conversation._state.execution_status = ( | ||
| conversation._state.execution_status.__class__.FINISHED | ||
| ) | ||
|
|
||
| # Capture logs at WARNING level | ||
| with caplog.at_level(logging.WARNING): | ||
| with patch( | ||
| "openhands.sdk.agent.agent.Agent.step", side_effect=finish_immediately | ||
| ): | ||
| # Run the conversation successfully | ||
| conversation.run() | ||
|
|
||
| # Close the conversation | ||
| conversation.close() | ||
|
|
||
| # Check that no warning about empty span stack was logged | ||
| warning_messages = [ | ||
| record.message for record in caplog.records if record.levelname == "WARNING" | ||
| ] | ||
| span_warnings = [ | ||
| msg | ||
| for msg in warning_messages | ||
| if "Attempted to end active span, but stack is empty" in msg | ||
| ] | ||
|
|
||
| assert len(span_warnings) == 0, f"Found span warnings: {span_warnings}" | ||
|
|
||
|
|
||
| def test_no_span_operations_when_observability_disabled(caplog): | ||
| """Test that no span operations occur when observability is disabled.""" | ||
|
|
||
| # Create test agent | ||
| agent = create_test_agent() | ||
|
|
||
| # Create a temporary workspace | ||
| with tempfile.TemporaryDirectory() as temp_dir: | ||
| # Create conversation | ||
| conversation = LocalConversation( | ||
| agent=agent, workspace=temp_dir, visualize=False | ||
| ) | ||
|
|
||
| # Mock the agent.step to finish immediately | ||
| def finish_immediately(*args, **kwargs): | ||
| conversation._state.execution_status = ( | ||
| conversation._state.execution_status.__class__.FINISHED | ||
| ) | ||
|
|
||
| # Capture logs at WARNING level | ||
| with caplog.at_level(logging.WARNING): | ||
| # Run and close the conversation | ||
| with patch( | ||
| "openhands.sdk.agent.agent.Agent.step", side_effect=finish_immediately | ||
| ): | ||
| conversation.run() | ||
| conversation.close() | ||
|
|
||
| # Check that no warning about empty span stack was logged | ||
| warning_messages = [ | ||
| record.message for record in caplog.records if record.levelname == "WARNING" | ||
| ] | ||
| span_warnings = [ | ||
| msg | ||
| for msg in warning_messages | ||
| if "Attempted to end active span, but stack is empty" in msg | ||
| ] | ||
|
|
||
| assert len(span_warnings) == 0, f"Found span warnings: {span_warnings}" |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wonder if this works normally with pydantic validation and serialization/deserialization. It should? But I'm a little unsure about redefining
__init__when we rely on some init/restore magic. 😓Maybe we could run all examples, including remote, to see all is good? I think we have a new workflow for that... WDYT?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have started the run here: https://github.com/OpenHands/software-agent-sdk/actions/runs/19106657774
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@enyst Looks like tests came back clean.