Skip to content

fix(tools): Gracefully handle missing PS1 metadata instead of crashing#2742

Open
VascoSch92 wants to merge 2 commits intomainfrom
fix/ps1-metadata-graceful-fallback
Open

fix(tools): Gracefully handle missing PS1 metadata instead of crashing#2742
VascoSch92 wants to merge 2 commits intomainfrom
fix/ps1-metadata-graceful-fallback

Conversation

@VascoSch92
Copy link
Copy Markdown
Contributor

@VascoSch92 VascoSch92 commented Apr 7, 2026

Summary

  • Replaces the hard assert in _handle_completed_command with a graceful fallback when no PS1 metadata markers are found in the terminal output
  • Returns a valid TerminalObservation with exit_code=-1 and a warning suffix instead of crashing the session
  • This happens when commands produce complex output (TUI rendering from libraries like ratatui, ANSI escape sequences, very large output) that corrupts or scrolls the ###PS1JSON###...###PS1END### markers off the screen buffer

Fixes #2416

Test plan

  • New test: test_issue_2416_missing_ps1_metadata_graceful_fallback
  • All 13 PS1 metadata tests pass
  • Manual: run cargo test in a Rust project with TUI components inside OpenHands

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:76ef39b-python

Run

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

All tags pushed for this build

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

About Multi-Architecture Support

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

When commands produce complex output (TUI rendering, ANSI escape
sequences, large output), the PS1 metadata markers can be corrupted
or scrolled off the terminal screen buffer. Previously this caused
a hard assert crash. Now the session returns a valid observation
with exit_code=-1 and a warning suffix, allowing the agent to
continue.

Fixes #2416

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@github-actions
Copy link
Copy Markdown
Contributor

github-actions bot commented Apr 7, 2026

Python API breakage checks — ✅ PASSED

Result:PASSED

Action log

@github-actions
Copy link
Copy Markdown
Contributor

github-actions bot commented Apr 7, 2026

REST API breakage checks (OpenAPI) — ✅ PASSED

Result:PASSED

Action log

@VascoSch92 VascoSch92 requested a review from all-hands-bot April 7, 2026 14:14
@VascoSch92 VascoSch92 changed the title Gracefully handle missing PS1 metadata instead of crashing fix(tools): Gracefully handle missing PS1 metadata instead of crashing Apr 7, 2026
Copy link
Copy Markdown
Collaborator

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

Choose a reason for hiding this comment

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

Pragmatic fix for a real crash. The fallback logic is sound and properly handles the edge case when PS1 metadata goes missing. Minor code duplication is acceptable given the simplicity trade-off.

@VascoSch92
Copy link
Copy Markdown
Contributor Author

@OpenHands fix the pre-commit check and follow the suggestion here #2742 (comment). Then push into this branch the changes.

@VascoSch92 VascoSch92 requested review from enyst and xingyaoww April 7, 2026 14:19
@openhands-ai
Copy link
Copy Markdown

openhands-ai bot commented Apr 7, 2026

I'm on it! VascoSch92 can track my progress at all-hands.dev

- Fix ruff format issues in terminal_session.py and test_terminal_ps1_metadata.py
- Update test_ps1_corruption.py tests to verify graceful fallback behavior
  instead of expecting AssertionError (matches new graceful handling logic)

Co-authored-by: openhands <openhands@all-hands.dev>
Copy link
Copy Markdown
Contributor Author

Fixed the pre-commit check and updated the tests in commit 8485fa5:

  1. Pre-commit formatting fixes:

    • Fixed ruff format issue in terminal_session.py (line 179) - CmdOutputMetadata call on single line
    • Fixed ruff format issue in test_terminal_ps1_metadata.py (line 382-384) - string concatenation on single line
  2. Test updates:

    • Updated test_handle_completed_command_fails_with_corrupted_outputtest_handle_completed_command_graceful_fallback_with_corrupted_output
    • Updated test_terminal_session_handles_corrupted_output_gracefully
    • Both tests now verify the graceful fallback behavior (returning a valid TerminalObservation with exit_code=-1) instead of expecting an AssertionError

The review comment at discussion r3045624320 suggested documenting the exit_code=-1 sentinel but concluded it was "acceptable as-is" since the suffix message explains the situation to users.


This comment was created by an AI assistant (OpenHands) on behalf of @VascoSch92.

@openhands-ai
Copy link
Copy Markdown

openhands-ai bot commented Apr 7, 2026

Summary of Work Completed

Request Checklist

Request Status
Fix the pre-commit check ✅ Complete
Follow the suggestion at discussion r3045624320 ✅ Complete (the suggestion concluded "acceptable as-is")
Push changes to the branch ✅ Complete

Changes Made (commit 8485fa5)

1. Pre-commit formatting fixes:

  • terminal_session.py: Single-line format for CmdOutputMetadata call
  • test_terminal_ps1_metadata.py: Single-line format for string concatenation

2. Test updates in test_ps1_corruption.py:

  • Updated 2 tests that were failing because they expected AssertionError to be raised
  • Tests now verify the graceful fallback behavior (exit_code=-1, valid TerminalObservation)

Conciseness Check

✅ All changes are directly related to fixing the CI failures - no extraneous changes were made.

Notes

  • The review comment (r3045624320) suggested documenting the exit_code=-1 sentinel but concluded it was "acceptable as-is" since the suffix message explains the situation to users. No additional code changes were needed for that suggestion.
  • All 24 PS1-related tests pass locally
  • CI checks are now running on the pushed commit

@github-actions
Copy link
Copy Markdown
Contributor

github-actions bot commented Apr 7, 2026

Coverage

Coverage Report •
FileStmtsMissCoverMissing
openhands-tools/openhands/tools/terminal/terminal
   terminal_session.py2037662%95, 101, 105–107, 143–144, 174, 179–180, 185, 191, 194–198, 213, 228–229, 270–272, 277, 280–281, 285, 291, 294, 309–311, 316, 319–320, 324, 330, 333, 353, 355, 358, 360, 376, 391, 397, 406, 409, 443, 447, 450, 453–454, 460–461, 467, 470, 477–478, 484–485, 544–546, 550, 555, 560–561, 565–566, 569–572, 578–579, 582
TOTAL220501109749% 

@all-hands-bot
Copy link
Copy Markdown
Collaborator

[Automatic Post]: This PR seems to be currently waiting for review. @enyst, @xingyaoww, could you please take a look when you have a chance?

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]: PS1 metadata parsing fails when running cargo test with large/complex stdout

3 participants