|
| 1 | +"""Test for the span double-ending issue in LocalConversation.""" |
| 2 | + |
| 3 | +import logging |
| 4 | +import tempfile |
| 5 | +from unittest.mock import patch |
| 6 | + |
| 7 | +import pytest |
| 8 | +from pydantic import SecretStr |
| 9 | + |
| 10 | +from openhands.sdk.agent import Agent |
| 11 | +from openhands.sdk.conversation.impl.local_conversation import LocalConversation |
| 12 | +from openhands.sdk.llm import LLM |
| 13 | + |
| 14 | + |
| 15 | +def create_test_agent() -> Agent: |
| 16 | + """Create a test agent.""" |
| 17 | + llm = LLM(model="gpt-4o-mini", api_key=SecretStr("test-key"), usage_id="test-llm") |
| 18 | + return Agent(llm=llm, tools=[]) |
| 19 | + |
| 20 | + |
| 21 | +def test_no_double_span_ending_warning(caplog): |
| 22 | + """Test that LocalConversation doesn't produce double span ending warnings.""" |
| 23 | + |
| 24 | + # Create test agent |
| 25 | + agent = create_test_agent() |
| 26 | + |
| 27 | + # Create a temporary workspace |
| 28 | + with tempfile.TemporaryDirectory() as temp_dir: |
| 29 | + # Create conversation |
| 30 | + conversation = LocalConversation( |
| 31 | + agent=agent, |
| 32 | + workspace=temp_dir, |
| 33 | + visualize=False, # Disable visualization to simplify test |
| 34 | + ) |
| 35 | + |
| 36 | + # Capture logs at WARNING level |
| 37 | + with caplog.at_level(logging.WARNING): |
| 38 | + # Mock the agent.step to raise an exception to trigger the finally block |
| 39 | + with patch( |
| 40 | + "openhands.sdk.agent.agent.Agent.step", |
| 41 | + side_effect=Exception("Test exception"), |
| 42 | + ): |
| 43 | + # Try to run the conversation (will fail due to mocked exception) |
| 44 | + with pytest.raises(Exception): |
| 45 | + conversation.run() |
| 46 | + |
| 47 | + # Close the conversation (this would normally be called by __del__) |
| 48 | + conversation.close() |
| 49 | + |
| 50 | + # Check that no warning about empty span stack was logged |
| 51 | + warning_messages = [ |
| 52 | + record.message for record in caplog.records if record.levelname == "WARNING" |
| 53 | + ] |
| 54 | + span_warnings = [ |
| 55 | + msg |
| 56 | + for msg in warning_messages |
| 57 | + if "Attempted to end active span, but stack is empty" in msg |
| 58 | + ] |
| 59 | + |
| 60 | + # This test should fail initially (showing the bug exists) |
| 61 | + # After the fix, there should be no span warnings |
| 62 | + assert len(span_warnings) == 0, f"Found span warnings: {span_warnings}" |
| 63 | + |
| 64 | + |
| 65 | +def test_span_ending_with_successful_run(caplog): |
| 66 | + """Test span ending behavior with a successful run (no exceptions).""" |
| 67 | + |
| 68 | + # Create test agent |
| 69 | + agent = create_test_agent() |
| 70 | + |
| 71 | + # Create a temporary workspace |
| 72 | + with tempfile.TemporaryDirectory() as temp_dir: |
| 73 | + # Create conversation |
| 74 | + conversation = LocalConversation( |
| 75 | + agent=agent, workspace=temp_dir, visualize=False |
| 76 | + ) |
| 77 | + |
| 78 | + # Mock the agent.step to finish immediately (no iterations) |
| 79 | + def finish_immediately(*args, **kwargs): |
| 80 | + conversation._state.execution_status = ( |
| 81 | + conversation._state.execution_status.__class__.FINISHED |
| 82 | + ) |
| 83 | + |
| 84 | + # Capture logs at WARNING level |
| 85 | + with caplog.at_level(logging.WARNING): |
| 86 | + with patch( |
| 87 | + "openhands.sdk.agent.agent.Agent.step", side_effect=finish_immediately |
| 88 | + ): |
| 89 | + # Run the conversation successfully |
| 90 | + conversation.run() |
| 91 | + |
| 92 | + # Close the conversation |
| 93 | + conversation.close() |
| 94 | + |
| 95 | + # Check that no warning about empty span stack was logged |
| 96 | + warning_messages = [ |
| 97 | + record.message for record in caplog.records if record.levelname == "WARNING" |
| 98 | + ] |
| 99 | + span_warnings = [ |
| 100 | + msg |
| 101 | + for msg in warning_messages |
| 102 | + if "Attempted to end active span, but stack is empty" in msg |
| 103 | + ] |
| 104 | + |
| 105 | + assert len(span_warnings) == 0, f"Found span warnings: {span_warnings}" |
| 106 | + |
| 107 | + |
| 108 | +def test_no_span_operations_when_observability_disabled(caplog): |
| 109 | + """Test that no span operations occur when observability is disabled.""" |
| 110 | + |
| 111 | + # Create test agent |
| 112 | + agent = create_test_agent() |
| 113 | + |
| 114 | + # Create a temporary workspace |
| 115 | + with tempfile.TemporaryDirectory() as temp_dir: |
| 116 | + # Create conversation |
| 117 | + conversation = LocalConversation( |
| 118 | + agent=agent, workspace=temp_dir, visualize=False |
| 119 | + ) |
| 120 | + |
| 121 | + # Mock the agent.step to finish immediately |
| 122 | + def finish_immediately(*args, **kwargs): |
| 123 | + conversation._state.execution_status = ( |
| 124 | + conversation._state.execution_status.__class__.FINISHED |
| 125 | + ) |
| 126 | + |
| 127 | + # Capture logs at WARNING level |
| 128 | + with caplog.at_level(logging.WARNING): |
| 129 | + # Run and close the conversation |
| 130 | + with patch( |
| 131 | + "openhands.sdk.agent.agent.Agent.step", side_effect=finish_immediately |
| 132 | + ): |
| 133 | + conversation.run() |
| 134 | + conversation.close() |
| 135 | + |
| 136 | + # Check that no warning about empty span stack was logged |
| 137 | + warning_messages = [ |
| 138 | + record.message for record in caplog.records if record.levelname == "WARNING" |
| 139 | + ] |
| 140 | + span_warnings = [ |
| 141 | + msg |
| 142 | + for msg in warning_messages |
| 143 | + if "Attempted to end active span, but stack is empty" in msg |
| 144 | + ] |
| 145 | + |
| 146 | + assert len(span_warnings) == 0, f"Found span warnings: {span_warnings}" |
0 commit comments