Repository navigation
fix(agent-server): skip restart-recovery error when observation already exists - #3063
Conversation
Python API breakage checks — ✅ PASSEDResult: ✅ PASSED |
REST API breakage checks (OpenAPI) — ✅ PASSEDResult: ✅ PASSED |
Coverage Report •
|
||||||||||||||||||||
all-hands-bot
left a comment
There was a problem hiding this comment.
🟢 Good taste - Clean bug fix with proper test coverage
[RISK ASSESSMENT]
Narrow bug fix in restart recovery path. Prevents duplicate observation-like events when an observation matches by tool_call_id but not action_id during restart. Well-tested with regression coverage. No impact on normal execution flow.
VERDICT:
✅ Worth merging - Fixes genuine bug in edge case handling
KEY INSIGHT:
Matching by tool_call_id instead of action_id during restart recovery is the right approach since action_ids can be rewritten on replay.
all-hands-bot
left a comment
There was a problem hiding this comment.
✅ QA Report: PASS
The fix successfully prevents duplicate observation-like events during restart recovery when an observation matches by tool_call_id but not action_id.
Does this PR achieve its stated goal?
Yes. This PR fixes issue #3045 by adding a guard in the restart recovery logic that checks for pre-existing observations before synthesizing an AgentErrorEvent. I verified the fix works by:
- Reproducing the bug scenario: Created a state with an unmatched action and a stale observation (same
tool_call_id, differentaction_id). Without the fix, this would produce duplicate observation-like events. - Verifying the fix: Confirmed that the new
already_observedcheck correctly prevents the duplicateAgentErrorEventwhen an observation already exists. - Testing the normal case: Verified that the fix still correctly synthesizes
AgentErrorEventwhen no observation exists (the normal crash recovery scenario).
| Phase | Result |
|---|---|
| Environment Setup | ✅ uv sync completed, Python 3.13.13 ready |
| CI Status | ✅ Core checks passing (Python API, REST API, check-docstrings) |
| Functional Verification | ✅ Fix prevents duplicates AND preserves normal restart behavior |
Functional Verification
Test 1: Reproduce issue #3045 (duplicate observation bug)
Step 1 — Establish the problematic scenario:
Created a crash recovery scenario with:
ActionEvent(tool_call_id=call_1, action_id=X)ObservationEvent(tool_call_id=call_1, action_id=Y)whereX != Y
This simulates what happens when:
- A tool starts executing
- Server crashes
- On restart, the observation arrives with a rewritten
action_id - The action appears "unmatched" to
get_unmatched_actions()even though an observation exists
Ran python verify_fix.py:
Step 2: ConversationState.get_unmatched_actions() reports:
- Found 1 unmatched action(s)
- Unmatched action: tool_call_id=call_1
Step 3: Apply the fix logic (lines 621-625 of event_service.py):
- Check: Does any ObservationBaseEvent exist for tool_call_id=call_1?
- Result: already_observed = True
✅ FIX WORKS: AgentErrorEvent will NOT be synthesized
This prevents the duplicate observation-like event that would
cause KeyError in condensation or rejection by LLM providers.
Interpretation: The fix correctly detects that an observation already exists for tool_call_id=call_1 and skips creating the duplicate error event.
Step 2 — Compare before/after behavior:
Ran python before_after_comparison.py to show the impact:
BEFORE THE FIX (old behavior):
4. Final state: 3 events
[1] ActionEvent (tool_call_id=call_1)
[2] ObservationEvent (tool_call_id=call_1)
[3] AgentErrorEvent (tool_call_id=call_1)
⚠️ PROBLEM: TWO observation-like events for tool_call_id=call_1!
💥 This causes:
• KeyError in condensation (manipulation_indices)
• Anthropic rejection: 'Found multiple tool_result blocks with id: call_1'
AFTER THE FIX (new behavior):
4. Final state: 2 events
[1] ActionEvent (tool_call_id=call_1)
[2] ObservationEvent (tool_call_id=call_1)
✅ FIXED: Only ONE observation-like event per tool_call_id
• No duplicate observation events
• Condensation works correctly
• LLM providers accept the conversation history
SUMMARY:
WITHOUT fix: 3 events (DUPLICATE observation-like events)
WITH fix: 2 events (clean 1:1 pairing)
Interpretation: The fix eliminates the duplicate observation-like events that caused condensation to crash with KeyError and LLM providers to reject the conversation.
Test 2: Verify normal restart case still works
Step 1 — Test crash recovery without pre-existing observation:
Created a scenario with only an unmatched action, no observation (the normal crash case).
Ran python verify_normal_case.py:
Step 3: Fix logic checks: already_observed = False
Step 4: ✅ AgentErrorEvent WAS synthesized (expected behavior)
- This is CORRECT: the tool was interrupted and never completed
- The agent needs to know about this failure
Final state: 2 events
[1] ActionEvent (tool_call_id=call_1)
[2] AgentErrorEvent (tool_call_id=call_1)
✅ VERIFICATION PASSED: Normal restart case still works correctly
The fix only skips AgentErrorEvent when an observation ALREADY EXISTS.
It still synthesizes AgentErrorEvent when the tool truly failed.
Interpretation: The fix preserves the normal restart behavior — when a tool genuinely fails due to a crash (no observation), the error event is still correctly synthesized.
Test 3: Regression test passes
Ran the new regression test added by this PR:
python -m pytest tests/agent_server/test_event_service.py -k "test_start_skips_error_event_when_observation_already_exists" -vResult:
tests/agent_server/test_event_service.py::TestEventServiceStartWithRunningStatus::test_start_skips_error_event_when_observation_already_exists PASSED [100%]
======================== 1 passed, 5 warnings in 0.20s =========================
Interpretation: The regression test correctly validates the fix.
Test 4: Related tests still pass
Ran all restart recovery tests to verify no regressions:
python -m pytest tests/agent_server/test_event_service.py::TestEventServiceStartWithRunningStatus -vResult:
test_start_sets_error_status_when_running_from_disk PASSED [ 25%]
test_start_does_not_add_error_event_when_no_unmatched_actions PASSED [ 50%]
test_start_does_nothing_when_status_not_running PASSED [ 75%]
test_start_skips_error_event_when_observation_already_exists PASSED [100%]
======================== 4 passed, 5 warnings in 0.20s =========================
Interpretation: All restart recovery tests pass, confirming no regressions.
Issues Found
None.
Why
Optional complementary patch for issue #3045
Summary
tool_call_id but not action_id and would otherwise produce a duplicate observation-like event.
Issue Number
#3045
How to Test
Type
Notes
This PR closes #3045
Agent Server images for this PR
• GHCR package: https://github.com/OpenHands/agent-sdk/pkgs/container/agent-server
Variants & Base Images
eclipse-temurin:17-jdknikolaik/python-nodejs:python3.13-nodejs22-slimgolang:1.21-bookwormPull (multi-arch manifest)
# Each variant is a multi-arch manifest supporting both amd64 and arm64 docker pull ghcr.io/openhands/agent-server:7d8b073-pythonRun
All tags pushed for this build
About Multi-Architecture Support
7d8b073-python) is a multi-arch manifest supporting both amd64 and arm647d8b073-python-amd64) are also available if needed