Skip to content

fix(agent-server): keep crash recovery result on interrupted action branch - #4488

Merged
neubig merged 4 commits into
mainfrom
test/crash-recovery-orphan-result
Aug 22, 2026
Merged

neubig merged 4 commits into
mainfrom
test/crash-recovery-orphan-result

Conversation

@neubig

@neubig neubig commented Aug 14, 2026 •

Copy link
Copy Markdown
Member

HUMAN:

This reproduces and fixes a restart-corruption bug seen in three real Agent Canvas conversations, using a file-backed lease-takeover test with no mocks in the failure path.


AGENT:

Why

Fixes #4487.

If the agent-server dies after an ActionEvent file is persisted but before base_state.json saves the advanced leaf_event_id, crash recovery correctly finds the interrupted action in the full event log but emits its synthetic AgentErrorEvent at the stale active HEAD. The action and result become siblings; the active branch contains a result without its tool call. The next context-preparation/condensation pass raises KeyError in ToolCallMatchingProperty.manipulation_indices, permanently breaking follow-up turns.

Three production conversations failed with this exact stack and distinct chatcmpl-tool-* IDs after one server restart.

Summary

  • Add an integration-style reproducer using real ConversationService, EventService, LocalConversation, file-backed EventLog, persisted HEAD, lease expiration/takeover, recovery emission, and follow-up handling.
  • Explicitly set crash recovery's synthetic AgentErrorEvent.parent_id to the interrupted ActionEvent.id.
  • Keep strict tool-call matching unchanged; repair event-tree lineage instead of suppressing invariant failures.

Issue Number

Fixes #4487.

How to Test

The test commit was run on current main before the fix commit and failed faithfully:

uv run pytest -q tests/cross/test_conversation_lease_behavior.py \
  -k crash_recovery_error_is_on_active_branch_for_next_turn -vv

Before:

recovered_state.view.manipulation_indices
.../tool_call_matching.py:93
    pending_tool_call_ids.remove(event.tool_call_id)
KeyError: 'chatcmpl-tool-crash-recovery'

After, on this PR with the same test unchanged:

test_live_lease_blocks_split_brain_resume_with_disk_events PASSED
test_expired_lease_takeover_fences_stale_writer_with_disk_events PASSED
test_crash_recovery_error_is_on_active_branch_for_next_turn PASSED
3 passed in 1.83s

Related strict invariant suite:

uv run pytest -q \
  tests/sdk/context/view/properties/test_tool_call_matching.py \
  tests/sdk/context/view/test_view_manipulation_indices.py \
  tests/cross/test_conversation_lease_behavior.py
# 33 passed in 3.81s

Video/Screenshots

Before terminal capture:

FAILED ...test_crash_recovery_error_is_on_active_branch_for_next_turn
KeyError: 'chatcmpl-tool-crash-recovery'

After terminal capture:

3 passed

Design Doc

Not included. The production change is one explicit parent_id assignment at the existing crash-recovery emission point.

Type

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

Notes

The failure path uses no mocks. Only the unavoidable process-crash atomicity window is simulated: after a real action append, persisted leaf_event_id is rewound to the preceding user message before a real lease takeover. The fix is a separate commit after the red test commit, preserving the before/after history in one PR.


🐳 Agent Server images for this PR — GHCR package, pull/run commands, and all pushed tags (click to expand)

• 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:6604560-python

Run

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

All tags pushed for this build

ghcr.io/openhands/agent-server:6604560-golang-amd64
ghcr.io/openhands/agent-server:660456096d8ffba5404d45287d10901c6e588f56-golang-amd64
ghcr.io/openhands/agent-server:test-crash-recovery-orphan-result-golang-amd64
ghcr.io/openhands/agent-server:6604560-golang_tag_1.21-bookworm-amd64
ghcr.io/openhands/agent-server:6604560-golang-arm64
ghcr.io/openhands/agent-server:660456096d8ffba5404d45287d10901c6e588f56-golang-arm64
ghcr.io/openhands/agent-server:test-crash-recovery-orphan-result-golang-arm64
ghcr.io/openhands/agent-server:6604560-golang_tag_1.21-bookworm-arm64
ghcr.io/openhands/agent-server:6604560-java-amd64
ghcr.io/openhands/agent-server:660456096d8ffba5404d45287d10901c6e588f56-java-amd64
ghcr.io/openhands/agent-server:test-crash-recovery-orphan-result-java-amd64
ghcr.io/openhands/agent-server:6604560-eclipse-temurin_tag_17-jdk-amd64
ghcr.io/openhands/agent-server:6604560-java-arm64
ghcr.io/openhands/agent-server:660456096d8ffba5404d45287d10901c6e588f56-java-arm64
ghcr.io/openhands/agent-server:test-crash-recovery-orphan-result-java-arm64
ghcr.io/openhands/agent-server:6604560-eclipse-temurin_tag_17-jdk-arm64
ghcr.io/openhands/agent-server:6604560-python-amd64
ghcr.io/openhands/agent-server:660456096d8ffba5404d45287d10901c6e588f56-python-amd64
ghcr.io/openhands/agent-server:test-crash-recovery-orphan-result-python-amd64
ghcr.io/openhands/agent-server:6604560-nikolaik_s_python-nodejs_tag_python3.13-nodejs22-slim-amd64
ghcr.io/openhands/agent-server:6604560-python-arm64
ghcr.io/openhands/agent-server:660456096d8ffba5404d45287d10901c6e588f56-python-arm64
ghcr.io/openhands/agent-server:test-crash-recovery-orphan-result-python-arm64
ghcr.io/openhands/agent-server:6604560-nikolaik_s_python-nodejs_tag_python3.13-nodejs22-slim-arm64
ghcr.io/openhands/agent-server:6604560-golang
ghcr.io/openhands/agent-server:660456096d8ffba5404d45287d10901c6e588f56-golang
ghcr.io/openhands/agent-server:test-crash-recovery-orphan-result-golang
ghcr.io/openhands/agent-server:6604560-golang_tag_1.21-bookworm
ghcr.io/openhands/agent-server:6604560-java
ghcr.io/openhands/agent-server:660456096d8ffba5404d45287d10901c6e588f56-java
ghcr.io/openhands/agent-server:test-crash-recovery-orphan-result-java
ghcr.io/openhands/agent-server:6604560-eclipse-temurin_tag_17-jdk
ghcr.io/openhands/agent-server:6604560-python
ghcr.io/openhands/agent-server:660456096d8ffba5404d45287d10901c6e588f56-python
ghcr.io/openhands/agent-server:test-crash-recovery-orphan-result-python
ghcr.io/openhands/agent-server:6604560-nikolaik_s_python-nodejs_tag_python3.13-nodejs22-slim

About Multi-Architecture Support

  • Each variant tag (e.g., 6604560-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., 6604560-python-amd64) are also available if needed

@github-actions

github-actions Bot commented Aug 14, 2026 •

Copy link
Copy Markdown
Contributor

Python API breakage checks — ✅ PASSED

Result: ✅ PASSED

Action log

@github-actions

github-actions Bot commented Aug 14, 2026 •

Copy link
Copy Markdown
Contributor

REST API breakage checks (OpenAPI) — ✅ PASSED

Result: ✅ PASSED

Action log

@neubig

neubig commented Aug 14, 2026

Copy link
Copy Markdown
Member Author

Issue #4487 now satisfies the repository readiness criteria and carries ready-for-dev; please re-run PR description validation.

@neubig neubig changed the title test(agent-server): reproduce restart corruption of tool-call view fix(agent-server): keep crash recovery result on interrupted action branch Aug 14, 2026
@neubig

neubig commented Aug 14, 2026

Copy link
Copy Markdown
Member Author

Combined as requested: this PR now has the faithful failing test commit followed by the minimal production fix commit. The same test fails at ae5b720b4 and passes unchanged at 697e1ed85.

@github-actions

github-actions Bot commented Aug 14, 2026 •

Copy link
Copy Markdown
Contributor

Coverage

Coverage Report •
FileStmtsMissCoverMissing
openhands-agent-server/openhands/agent_server
   event_service.py85812685%162–163, 289, 293, 298, 325, 332, 366, 369–370, 374–375, 386, 392, 402–406, 409–412, 483, 504–505, 579, 633, 653, 660, 684–685, 689, 697, 700, 716, 748, 759, 766, 772, 837–838, 916, 946, 949, 1009, 1030, 1161, 1273–1276, 1280, 1309, 1313, 1320, 1334, 1349, 1396–1398, 1478, 1503, 1509, 1511, 1521, 1523, 1530, 1540, 1542–1543, 1547, 1561–1566, 1568, 1595, 1600–1606, 1610–1613, 1621–1624, 1670–1672, 1726–1727, 1729–1736, 1738–1739, 1748–1749, 1751–1752, 1759–1760, 1762–1763, 1774, 1799, 1805, 1811, 1820–1821, 1843, 1853
TOTAL407451716758% 

@all-hands-bot

Copy link
Copy Markdown
Collaborator

🚦 CI is currently failing on this PR's latest commit.

Please fix the failing checks before OpenHands reviews it - this is re-checked automatically once you push a new commit. (A maintainer can also request @all-hands-bot as a reviewer to have it reviewed regardless of CI status.)

This is an automated check - no AI was used to generate this comment.

@all-hands-bot

Copy link
Copy Markdown
Collaborator

🤖 OpenHands is reviewing this PR.

Head commit: 660456096d8ffba5404d45287d10901c6e588f56
View the conversation: https://oss-agent-canvas.ngrok.dev/conversations/92997088-90e6-4406-b8d5-9467be9cf4f5

This comment was posted by an AI agent (OpenHands).

@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.

This review was created by an AI agent (OpenHands) on behalf of the repository maintainers.

Summary

This PR fixes a real crash-recovery bug where a synthetic AgentErrorEvent emitted after a server restart could land as a sibling of its interrupted ActionEvent instead of as its child, leaving an orphan tool result on the active branch and causing a KeyError in ToolCallMatchingProperty.manipulation_indices on the next turn.

The root cause is sound: when the process dies after appending an ActionEvent file but before base_state.json saves the advanced leaf_event_id, the persisted HEAD is stale. Crash recovery then creates an AgentErrorEvent with parent_id=None, and _stamp_parent_id attaches it to _resolve_active_leaf() — the stale HEAD (the preceding user message) — rather than to the action. The action and error become siblings, so the active branch (path_to_root from the error leaf) never includes the action, and manipulation_indices sees an ObservationBaseEvent with no preceding ActionEvent.

The fix — explicitly setting parent_id=first_action.id — is correct and minimal. _stamp_parent_id returns early when parent_id is already set, so the explicit lineage is preserved through append_event. This ensures path_to_root from the error leaf traverses error -> action -> user_message -> system_prompt, giving manipulation_indices a properly paired action/observation boundary.

Risk Assessment: LOW

  • The change is a single parent_id assignment in a recovery-only code path (conversation loaded with stale RUNNING status after restart).
  • Strict tool-call matching invariants are unchanged; the fix repairs event-tree lineage rather than suppressing invariant failures.
  • The integration test uses real ConversationService, EventService, LocalConversation, file-backed EventLog, persisted HEAD, lease expiration/takeover, and follow-up message handling — no mocks in the failure path.
  • All 33 tests in the related strict invariant suite pass.

No Material Findings

No bugs, security issues, or design flaws found. The fix is well-scoped and the test faithfully reproduces the production ordering.

One minor cosmetic note (not blocking): the test docstring describes the bug in present tense ("The current implementation writes the recovery error as a sibling..."), which is no longer accurate after the fix. This does not affect correctness.

@neubig

neubig commented Aug 15, 2026

Copy link
Copy Markdown
Member Author

@all-hands-bot Please re-review the current HEAD and provide a positive approval if no material findings remain. This request was posted by an AI agent (OpenHands) on behalf of the user.

@neubig
neubig requested review from all-hands-bot and removed request for all-hands-bot August 17, 2026 21:33

all-hands-bot commented Aug 22, 2026 •

Copy link
Copy Markdown
Collaborator

✅ Review complete.

This review was performed through OpenHands Cloud Automation. You can log in and view the conversation here.

@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

This is the right data-structure fix: repair lineage at write time instead of weakening tool-call matching. Explicitly parenting the synthetic recovery result to the interrupted action handles a stale persisted HEAD while preserving the existing event invariants.

I found no blocking issues. I ran:

uv run pytest -q tests/cross/test_conversation_lease_behavior.py
# 3 passed

[RISK ASSESSMENT]

  • [Overall PR] ⚠️ Risk Assessment: 🟢 LOW
    Recovery-only code path, one parent_id assignment, and regression coverage for the file-backed crash window plus next-turn view construction. No API, dependency, or normal run-loop behavior changes.

VERDICT:
✅ Worth merging: Core logic is sound, targeted regression coverage exists, and CI is passing.

KEY INSIGHT:
The durable invariant is the event tree lineage; fixing the synthetic recovery result’s parent keeps strict tool-call matching honest without special-casing the condenser.

This review was generated by an AI agent (OpenHands) on behalf of the user through OpenHands Automation. View conversation

@neubig
neubig merged commit 88afa9a into main Aug 22, 2026
42 checks passed
@neubig
neubig deleted the test/crash-recovery-orphan-result branch August 22, 2026 13:13
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.

[Bug]: crash recovery orphans interrupted tool result and permanently breaks subsequent turns

3 participants