Skip to content

fix(cli): surface fatal conversation errors and exit non-zero in headless/json mode - #846

Open
zsicheng0214 wants to merge 1 commit into
OpenHands:mainfrom
TraceLite-AI:fix/headless-error-exit-code
Open

zsicheng0214 wants to merge 1 commit into
OpenHands:mainfrom
TraceLite-AI:fix/headless-error-exit-code

Conversation

@zsicheng0214

Copy link
Copy Markdown

Summary

In --headless (and --headless --json) mode, ConversationRunner._execute_conversation() catches ConversationRunError and unexpected Exceptions and forwards them only to _notification_callback (a Textual UI toast). In headless mode:

  1. Nothing is written to stderr and no error event is emitted to the --json JSONL stdout stream;
  2. finally: self._update_run_status(False) triggers ConversationFinished;
  3. OpenHandsApp.on_conversation_finished() unconditionally calls self.exit() (return_code=0), causing openhands --headless to print Goodbye! 👋 and exit with code 0 even on fatal errors such as upstream HTTP 402 or Agent._initialize() ValueError.

This breaks CI/CD pipelines, batch automation, and benchmark runners that rely on exit codes and JSONL error events.

Changes

  • openhands_cli/tui/core/state.py: Add last_error property and set_error(error) on ConversationContainer to track the fatal error from the most recent run.
  • openhands_cli/tui/core/conversation_runner.py: Clear state.set_error(None) before each run in _execute_conversation() and record state.set_error(e) when catching ConversationRunError or unexpected Exception.
  • openhands_cli/tui/textual_app.py:
    • Persist self.json_mode = json_mode on OpenHandsApp.
    • In on_conversation_finished(), when headless_mode=True and conversation_state.last_error is set, emit a structured JSONL ConversationErrorEvent (in --json mode) and write the error to sys.stderr, then call self.exit(return_code=1).
    • In textual_main(), propagate non-zero integer app.return_code via sys.exit(app.return_code) in headless mode.
  • tests/tui/test_headless_mode.py: Add 6 unit tests covering ConversationRunError, unexpected Exception, stderr + JSONL emission, clean-run zero exit, and textual_main() exit code propagation.

Verification

  • uv run pytest tests/tui/test_headless_mode.py tests/test_main.py -q → 65 passed
  • uv run ruff check & uv run ruff format --check → All checks passed
  • uv run pyright on all modified source files → 0 errors, 0 warnings

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.

1 participant