Skip to content

fix: improve conversation resilience for long-running and resumed sessions - #2384

Merged
xingyaoww merged 2 commits into
mainfrom
fix/conversation-resilience-improvements
Mar 11, 2026
Merged

xingyaoww merged 2 commits into
mainfrom
fix/conversation-resilience-improvements

Conversation

@xingyaoww

@xingyaoww xingyaoww commented Mar 11, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Three targeted fixes for issues discovered while running headless benchmark sessions (via openhands-ai SDK) with non-OpenAI models (kimi-k2.5, minimax-m2.5). These bugs cause conversations to become permanently "poisoned" — unable to recover even after resume — in long-running or multi-session scenarios.

Problem

Users running the SDK in headless/benchmark mode with certain LLMs encounter three classes of session-killing bugs:

  1. Malformed tool_call.arguments: Some models emit raw control characters (literal \n, \t, NUL bytes) instead of JSON escape sequences (\\n, \\t). json.loads() rejects these per RFC 8259, causing an AgentErrorEvent that poisons the conversation permanently.

  2. Stale EventLog in-memory index: When the in-memory _idx_to_id mapping becomes stale (e.g., after external file modifications, concurrent writes, or event healing), _path(i) raises KeyError, crashing the session instead of recovering.

  3. STUCK status permanently kills sessions: ConversationExecutionStatus.STUCK is a terminal state — send_message() doesn't reset it and run() exits immediately. If a STUCK state is persisted to disk, the conversation can never recover on resume.

Changes

Fix 1: Sanitize control characters in tool_call.arguments

Files: agent/utils.py, agent/agent.py

  • Added sanitize_json_control_chars() utility that escapes raw U+0000–U+001F bytes to their JSON-legal equivalents (\n, \t, \uXXXX)
  • Applied before json.loads(tool_call.arguments) in _get_action_event()
  • Analogous to the existing fix_malformed_tool_arguments() but operates at the character level

Fix 2: EventLog stale index recovery

Files: conversation/event_store.py

  • In _get_single_item(): catch KeyError from _path(), rebuild index from disk via _scan_and_build_index(), retry once
  • Falls back to IndexError if the index still doesn't exist after rebuild (graceful degradation)

Fix 3: STUCK status recovery on resume

Files: conversation/impl/local_conversation.py

  • send_message(): now resets STUCK → IDLE (like FINISHED → IDLE), treating a new user message as an implicit "unstick" signal
  • run(): includes STUCK in the pre-loop status reset (alongside IDLE, PAUSED, ERROR)

Tests

  • 9 new tests for sanitize_json_control_chars() covering: valid JSON passthrough, literal newline/tab/NUL/CR, multi-char combinations, already-escaped sequences, empty input, realistic tool_call payloads
  • 2 new tests for EventLog stale index recovery: successful recovery and IndexError after rebuild
  • 2 updated tests for STUCK status transitions: test_run_recovers_from_stuck and test_send_message_resets_stuck_to_idle
  • All 234 SDK-specific tests pass; pre-existing failures in agent_server/browser_use are unrelated

Context

These issues were discovered by a user running the SDK for the AgentBench benchmark harness. They had to maintain 5 monkey-patches downstream to work around these bugs. This PR upstreams 3 of those fixes (the ones with clear root causes in the SDK). The remaining 2 patches (conversation healer for poisoned events and ToolCallMatchingProperty.manipulation_indices using set.remove() on orphaned IDs) may warrant separate PRs after further investigation.


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 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:6190e69-python

Run

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

All tags pushed for this build

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

About Multi-Architecture Support

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

…sions

Three targeted fixes for issues discovered while running headless
benchmark sessions with non-OpenAI models (kimi-k2.5, minimax-m2.5):

1. Sanitize raw control characters in tool_call.arguments (agent.py)
   Some LLMs emit literal U+0000–U+001F bytes instead of JSON escape
   sequences (\n, \t, etc.) in tool_call.arguments.  json.loads()
   rejects these per RFC 8259, poisoning the session permanently.
   Added sanitize_json_control_chars() to escape them before parsing.

2. EventLog._get_single_item: recover from stale in-memory index
   When the in-memory index (_idx_to_id) becomes stale—e.g. after
   external file modifications or concurrent writes—_path(i) raises
   KeyError.  Now catches KeyError, rebuilds the index from disk via
   _scan_and_build_index(), and retries once before raising IndexError.

3. Allow STUCK conversations to resume on new user message or run()
   Previously STUCK was a terminal state: send_message() did not reset
   it and run() exited immediately.  A persisted STUCK state would
   permanently kill the session on resume.  Now:
   - send_message() resets STUCK → IDLE (like FINISHED → IDLE)
   - run() includes STUCK in its pre-loop status reset

Co-authored-by: openhands <openhands@all-hands.dev>
@github-actions

github-actions Bot commented Mar 11, 2026 •

Copy link
Copy Markdown
Contributor

API breakage checks (Griffe)

Result: Passed

Action log

@github-actions

github-actions Bot commented Mar 11, 2026 •

Copy link
Copy Markdown
Contributor

Agent server REST API breakage checks (OpenAPI)

Result: Failed

Log excerpt (first 1000 characters)
{"asctime": "2026-03-11 18:07:39,222", "levelname": "WARNING", "name": "openhands.agent_server.config", "filename": "config.py", "lineno": 173, "message": "\u26a0\ufe0f OH_SECRET_KEY was not defined. Secrets will not be persisted between restarts."}
::error title=openhands-agent-server REST API::Breaking REST API change detected without MINOR version bump (1.13.0 -> 1.13.0).

Breaking REST API changes detected compared to baseline release:
- the 'file' request property type/format changed from 'string'/'' to 'string'/'binary'
/home/runner/work/software-agent-sdk/software-agent-sdk/.venv/lib/python3.13/site-packages/litellm/llms/custom_httpx/async_client_cleanup.py:66: DeprecationWarning: There is no current event loop
  loop = asyncio.get_event_loop()

Action log

@github-actions

github-actions Bot commented Mar 11, 2026 •

Copy link
Copy Markdown
Contributor

Coverage

Coverage Report •
FileStmtsMissCoverMissing
openhands-sdk/openhands/sdk/agent
   agent.py2483685%95, 99, 279–281, 283, 313–314, 321–322, 354, 407–408, 410, 450, 598–599, 604, 616–617, 622–623, 642–643, 645, 673–674, 681–682, 686, 694–695, 732, 738, 750, 757
   utils.py65395%99, 119–120
openhands-sdk/openhands/sdk/conversation
   event_store.py1441788%94, 101, 105–106, 138, 142, 148, 150–153, 173–176, 219, 232
openhands-sdk/openhands/sdk/conversation/impl
   local_conversation.py3942394%288, 293, 321, 364, 382, 398, 463, 641–642, 645, 797, 805, 807, 818, 820–822, 847, 1040, 1044, 1114, 1121–1122
TOTAL19930579170% 

@neubig neubig left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks a lot for this!

@xingyaoww
xingyaoww marked this pull request as ready for review March 11, 2026 13:45
@xingyaoww
xingyaoww requested a review from all-hands-bot March 11, 2026 18:07

@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 - Elegant, focused fixes for real production bugs.

Three pragmatic bug fixes discovered in benchmark runs with non-OpenAI models. Clean implementations, well-tested, no unnecessary complexity:

  • Control char sanitization: Simple regex transformation to make malformed LLM output valid JSON
  • EventLog recovery: Defensive one-time index rebuild on stale access
  • STUCK recovery: Treats STUCK like ERROR - recoverable, not permanently terminal

Testing: 13 new/updated tests covering real behavior (parsing JSON, loading events, transitioning states). No issues found.

Verdict: ✅ Approve - Solid bug fixes that make broken sessions recoverable without changing core architecture.

@xingyaoww
xingyaoww merged commit 46f3d78 into main Mar 11, 2026
29 checks passed
@xingyaoww
xingyaoww deleted the fix/conversation-resilience-improvements branch March 11, 2026 18:28
StressTestor pushed a commit to StressTestor/software-agent-sdk that referenced this pull request Jun 1, 2026
…sions (OpenHands#2384)

Co-authored-by: openhands <openhands@all-hands.dev>
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.

4 participants