Repository navigation
fix(agent-server): keep crash recovery result on interrupted action branch - #4488
Conversation
Python API breakage checks — ✅ PASSEDResult: ✅ PASSED |
REST API breakage checks (OpenAPI) — ✅ PASSEDResult: ✅ PASSED |
|
Issue #4487 now satisfies the repository readiness criteria and carries |
|
Combined as requested: this PR now has the faithful failing test commit followed by the minimal production fix commit. The same test fails at |
Coverage Report •
|
||||||||||||||||||||
|
🚦 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 This is an automated check - no AI was used to generate this comment. |
|
🤖 OpenHands is reviewing this PR. Head commit: This comment was posted by an AI agent (OpenHands). |
all-hands-bot
left a comment
There was a problem hiding this comment.
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_idassignment 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-backedEventLog, 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.
|
@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. |
|
✅ Review complete. This review was performed through OpenHands Cloud Automation. You can log in and view the conversation here. |
all-hands-bot
left a comment
There was a problem hiding this comment.
🟢 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, oneparent_idassignment, 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
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
ActionEventfile is persisted but beforebase_state.jsonsaves the advancedleaf_event_id, crash recovery correctly finds the interrupted action in the full event log but emits its syntheticAgentErrorEventat 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 raisesKeyErrorinToolCallMatchingProperty.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
ConversationService,EventService,LocalConversation, file-backedEventLog, persisted HEAD, lease expiration/takeover, recovery emission, and follow-up handling.AgentErrorEvent.parent_idto the interruptedActionEvent.id.Issue Number
Fixes #4487.
How to Test
The test commit was run on current main before the fix commit and failed faithfully:
Before:
After, on this PR with the same test unchanged:
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.81sVideo/Screenshots
Before terminal capture:
After terminal capture:
Design Doc
Not included. The production change is one explicit
parent_idassignment at the existing crash-recovery emission point.Type
Notes
The failure path uses no mocks. Only the unavoidable process-crash atomicity window is simulated: after a real action append, persisted
leaf_event_idis 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
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:6604560-pythonRun
All tags pushed for this build
About Multi-Architecture Support
6604560-python) is a multi-arch manifest supporting both amd64 and arm646604560-python-amd64) are also available if needed