Skip to content

fix(agent-server): skip restart-recovery error when observation already exists - #3063

Merged
VascoSch92 merged 3 commits into
mainfrom
vasco/complementary-patch
May 5, 2026
Merged

VascoSch92 merged 3 commits into
mainfrom
vasco/complementary-patch

Conversation

@VascoSch92

@VascoSch92 VascoSch92 commented May 5, 2026 •

Copy link
Copy Markdown
Member
  • A human has tested these changes.

Why

Optional complementary patch for issue #3045

Summary

  • In event_service.py restart recovery, skip the synthesized AgentErrorEvent when the loaded state already carries any ObservationBaseEvent for the unmatched action's tool_call_id. Closes the gap where an observation matches by
    tool_call_id but not action_id and would otherwise produce a duplicate observation-like event.
  • Complements the cardinality-aware enforce() and soft manipulation_indices() already in tool_call_matching.py: stops new bad histories at the source while those guards self-heal histories already on disk.

Issue Number

#3045

How to Test

  • added a regression test for the changes

Type

  • Bug fix
  • Feature
  • Refactor
  • Breaking change
  • Docs / chore

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

Variant Architectures Base Image Docs / Tags
java amd64, arm64 eclipse-temurin:17-jdk Link
python amd64, arm64 nikolaik/python-nodejs:python3.13-nodejs22-slim Link
golang amd64, arm64 golang:1.21-bookworm Link

Pull (multi-arch manifest)

# Each variant is a multi-arch manifest supporting both amd64 and arm64
docker pull ghcr.io/openhands/agent-server:7d8b073-python

Run

docker run -it --rm \
  -p 8000:8000 \
  --name agent-server-7d8b073-python \
  ghcr.io/openhands/agent-server:7d8b073-python

All tags pushed for this build

ghcr.io/openhands/agent-server:7d8b073-golang-amd64
ghcr.io/openhands/agent-server:7d8b073-golang_tag_1.21-bookworm-amd64
ghcr.io/openhands/agent-server:7d8b073-golang-arm64
ghcr.io/openhands/agent-server:7d8b073-golang_tag_1.21-bookworm-arm64
ghcr.io/openhands/agent-server:7d8b073-java-amd64
ghcr.io/openhands/agent-server:7d8b073-eclipse-temurin_tag_17-jdk-amd64
ghcr.io/openhands/agent-server:7d8b073-java-arm64
ghcr.io/openhands/agent-server:7d8b073-eclipse-temurin_tag_17-jdk-arm64
ghcr.io/openhands/agent-server:7d8b073-python-amd64
ghcr.io/openhands/agent-server:7d8b073-nikolaik_s_python-nodejs_tag_python3.13-nodejs22-slim-amd64
ghcr.io/openhands/agent-server:7d8b073-python-arm64
ghcr.io/openhands/agent-server:7d8b073-nikolaik_s_python-nodejs_tag_python3.13-nodejs22-slim-arm64
ghcr.io/openhands/agent-server:7d8b073-golang
ghcr.io/openhands/agent-server:7d8b073-java
ghcr.io/openhands/agent-server:7d8b073-python

About Multi-Architecture Support

  • Each variant tag (e.g., 7d8b073-python) is a multi-arch manifest supporting both amd64 and arm64
  • Docker automatically pulls the correct architecture for your platform
  • Individual architecture tags (e.g., 7d8b073-python-amd64) are also available if needed

@github-actions

github-actions Bot commented May 5, 2026 •

Copy link
Copy Markdown
Contributor

Python API breakage checks — ✅ PASSED

Result: ✅ PASSED

Action log

@github-actions

github-actions Bot commented May 5, 2026 •

Copy link
Copy Markdown
Contributor

REST API breakage checks (OpenAPI) — ✅ PASSED

Result: ✅ PASSED

Action log

@github-actions

github-actions Bot commented May 5, 2026 •

Copy link
Copy Markdown
Contributor

Coverage

Coverage Report •
FileStmtsMissCoverMissing
openhands-agent-server/openhands/agent_server
   event_service.py4109776%73–74, 99, 103, 106–107, 111–112, 118–120, 129–133, 136–139, 159, 263, 280, 321, 331, 355–356, 360, 368, 371, 411–412, 428, 430, 434–436, 440, 449–450, 452, 456, 462, 464, 494–499, 549, 553, 699, 701–702, 706, 720–722, 724, 728–731, 735–738, 746–749, 780–781, 783–790, 792–793, 802–803, 805–806, 813–814, 816–817, 837, 843, 849, 858–859
TOTAL248471121954% 

@all-hands-bot all-hands-bot left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Good taste - Clean bug fix with proper test coverage

[RISK ASSESSMENT]
⚠️ Risk Assessment: 🟢 LOW

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 all-hands-bot left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ 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:

  1. Reproducing the bug scenario: Created a state with an unmatched action and a stale observation (same tool_call_id, different action_id). Without the fix, this would produce duplicate observation-like events.
  2. Verifying the fix: Confirmed that the new already_observed check correctly prevents the duplicate AgentErrorEvent when an observation already exists.
  3. Testing the normal case: Verified that the fix still correctly synthesizes AgentErrorEvent when 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) where X != Y

This simulates what happens when:

  1. A tool starts executing
  2. Server crashes
  3. On restart, the observation arrives with a rewritten action_id
  4. 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" -v

Result:

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 -v

Result:

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.

@csmith49 csmith49 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔥

@VascoSch92
VascoSch92 enabled auto-merge (squash) May 5, 2026 15:37
@VascoSch92
VascoSch92 merged commit b6c63d0 into main May 5, 2026
26 checks passed
@VascoSch92
VascoSch92 deleted the vasco/complementary-patch branch May 5, 2026 15:40
StressTestor pushed a commit to StressTestor/software-agent-sdk that referenced this pull request Jun 1, 2026
pengzhang-create pushed a commit to Pyromind-Dynamics/software-agent-sdk that referenced this pull request Aug 13, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Crash recovery can create duplicate tool results and crash condensation with KeyError

3 participants