Skip to content

fix(agent-server): reconcile stale goal status on conversation reload - #5719

Closed
kyletser wants to merge 1 commit into
OpenHands:mainfrom
kyletser:fix/goal-loop-restart-interrupted-5627
Closed

kyletser wants to merge 1 commit into
OpenHands:mainfrom
kyletser:fix/goal-loop-restart-interrupted-5627

Conversation

@kyletser

Copy link
Copy Markdown
Contributor

HUMAN:


AGENT:

Why

Fixes #5627. When the agent server stops while a /goal loop is active, the loop is cancelled but no terminal goal status is recorded: a graceful shutdown cancels it inside close() with _closing set (the cancel handler deliberately records nothing), and a hard kill never runs close() at all. After the restart the last persisted goal status still says active: true, status: "running" while no loop exists, so a UI shows a running goal indefinitely and /goal/stop cannot clear it (there is no task to cancel).

Summary

  • EventService.start() now reconciles the loaded state next to the existing stale-RUNNING execution-status reconcile: when the last persisted goal status is active: true, it appends a goal ConversationStateUpdateEvent with the same objective, iteration, max_iterations, and verdict, and active: false, status: "interrupted" — the same terminal record /goal/stop writes.
  • The reconciliation is driven purely by the loaded persisted state, so it covers both a graceful restart and a hard kill alike.
  • The state-lock acquisition runs in a worker thread (run_in_executor), per the agent-server concurrency rules in openhands-agent-server/AGENTS.md.
  • Terminal statuses (complete/capped) and conversations without any goal status are left untouched; the goal event value shape is unchanged, so no schema or REST contract change.

Issue Number

#5627

How to Test

  • uv run pytest tests/agent_server/test_goal_loop.py — 23 passed, including three new regression tests:
    • test_restart_reconciles_stale_running_goal_status — hard-kill simulation: a persisted active: true status is reconciled to interrupted with identical fields, exactly one new event is appended, and /goal/resume is admitted afterwards and drives the loop to complete.
    • test_goal_loop_graceful_shutdown_reconciles_on_restart — a real loop is caught mid-audit, close() tears the service down (no status recorded), and the reload reconciles the stale status.
    • test_restart_preserves_terminal_goal_status — a complete status gains no extra event on reload, and a conversation without goal events stays goal-event-free.
  • Full local runs (WSL, .venv): tests/agent_server/test_goal_loop.py, test_event_service.py, test_switch_llm_survives_reload.py, test_run_admission.py, test_conversation_lease.py, test_conversation_service.py all green; ruff check + ruff format --check and pyright clean on the changed files.
  • TDD: the two reconcile tests fail on main and pass with the fix.

Video/Screenshots

Not a visual change; the How to Test section covers the observable behavior via the goal-status events.

Design Doc

Small, single-site fix; no design doc needed.

Type

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

Notes

The issue's alternative (recording interrupted in the cancel handler during close()) would not cover a hard kill, which is why the fix reconciles in start() from the loaded state, as the triage suggested. The F11.goal-restart-active map bullet lives in the still-open verify-agent-server PR #5621, so it is out of scope here.

…OpenHands#5627)

A /goal loop that was in flight when the process stopped left its last
persisted goal status active=True ("running"): a graceful shutdown cancels
the loop inside close() with _closing set (the cancel handler then records
nothing), and a hard kill never runs close() at all. After the restart no
loop exists, yet the stale status made a UI render a dead goal as running
forever and /goal/stop could not clear it.

EventService.start() now reconciles the loaded state: when the last
persisted goal status is active, it appends a "goal" state-update event
with the same objective/iteration/max_iterations/verdict and
active=False, status="interrupted" -- the same terminal record /goal/stop
writes -- keeping the goal resumable via /goal/resume. The state-lock
acquisition runs in a worker thread per the agent-server concurrency
rules.

Terminal statuses (complete/capped) and conversations without goal events
are left untouched.

Co-authored-by: openhands <openhands@all-hands.dev>
Copilot AI balanced review requested due to automatic review settings October 12, 2026 09:56

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

@neubig

neubig commented Oct 14, 2026

Copy link
Copy Markdown
Member

Closing in favour of #5772, which fixes the same issue (#5627) and has been approved. The core approach here — reconciling the stale goal status in start() from loaded state so both graceful restarts and hard kills are covered — is the same one #5772 takes, and your graceful + hard-kill regression tests are solid. #5772 was chosen because it is current with fully green CI (this PR currently has a failing PR-description check), adds live map evidence (xfail on main -> xpass -> pass), validates the persisted status via GoalStatus and clears verdict like other interrupted statuses, cannot block conversation loading on an unreadable log, and removes the fixed known-bug marker from the feature map. Please reopen or comment if you spot a gap in #5772.

This comment was created by an AI agent (OpenHands) on behalf of Graham Neubig.

@neubig neubig closed this Oct 14, 2026
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]: /goal loop active at a graceful restart stays running afterwards instead of interrupted

3 participants