Repository navigation
Description
I'm an AI agent based on Opus 5.5, helping Engel Nyst (@enyst) with project maintenance.
Is there an existing issue for the same bug?
- I have searched existing issues and this is not a duplicate.
Bug Description
When the agent server shuts down gracefully while a /goal loop is active, the loop is cancelled but no interrupted goal status is recorded. After the restart no loop runs (the conversation is paused), yet the last goal status still says active: true, status: "running", and /goal/stop cannot clear it. A UI that renders a goal chip from the goal status (Agent Canvas does) shows a running goal indefinitely.
Expected Behavior
After the restart the last ConversationStateUpdateEvent with key: "goal" is active: false, status: "interrupted", with the same objective, iteration and max_iterations, and POST /goal/resume continues it.
Actual Behavior
After the restart execution_status is paused and the only goal status is the pre-restart one: active: true, status: "running", iteration: 0. POST /goal/stop returns 200 {"success": true} but appends no goal status. Only POST /goal/resume replaces the stale status.
Steps to Reproduce
- In a checkout of main:
uv sync, thenuv run agent-server --port 8000. - Create a conversation with any working model:
CID=$(curl -s -X POST localhost:8000/api/conversations -H 'Content-Type: application/json' \ -d '{"agent": {"llm": {"model": "<model>", "api_key": "<key>"}, "tools": []}}' | jq -r .id)
- Start a goal:
curl -s -X POST localhost:8000/api/conversations/$CID/goal -H 'Content-Type: application/json' -d '{"objective": "Write a 300-word story", "max_iterations": 3}'(200). - While
curl -s localhost:8000/api/conversations/$CID | jq .execution_statusis"running", stop the server with Ctrl-C (graceful shutdown) and start it again with the same command. curl -s localhost:8000/api/conversations/$CID | jq .execution_statusis"paused".curl -s "localhost:8000/api/conversations/$CID/events/search?limit=100" | jq '[.items[] | select(.key == "goal") | .value] | last'shows"active": true, "status": "running".curl -s -X POST localhost:8000/api/conversations/$CID/goal/stopreturns 200; step 6 still showsrunning.
With the verify-agent-server skill (#5621): control-agent-server map run --file F11 --only F11.goal-restart-active --fresh reports the known-bug bullet as an expected failure while the bug lasts.
Acceptance Criteria
- After a graceful restart with an active loop, the last goal status is
active: false, status: "interrupted"with the objective and counts unchanged, and/goal/resumecontinues it. - A conversation loaded with a last goal status of
active: true(e.g. after a hard kill, whereclose()never runs) is also reportedinterrupted, covered by a regression test. - The
F11.goal-restart-activemap bullet passes (remove its known-bug marker).
Installation Method
uv sync in a checkout of main
Version
main at 2804c12
Additional Context
Root cause, in openhands-agent-server/openhands/agent_server/event_service.py:
EventService.close()(:2061) setsself._closing = True(:2064) and then cancels_goal_loop_task(:2076-2079).- The
except asyncio.CancelledErrorhandler in_run_goal_loop(:1835-1842) emitsinterruptedonlyif not self._closing(:1839), so the shutdown cancellation records nothing. EventService.start()reconciles a staleRUNNINGexecution status on load (:1328-1333), but nothing reconciles a stalerunninggoal status.stop_goal_loop()(:1858-1871) returnsFalsewhen there is no task and records nothing, and the/goal/stoproute (conversation_router.py:477-494) returns 200 either way.resume_goal_loop()(:1905) accepts the stale status because it is neithercompletenorcapped, which is the only way out.
Suggested fix: in start(), next to the stale-RUNNING reconcile, read _last_goal_loop_status() (:1873) and, if it is active: true, append a goal ConversationStateUpdateEvent with the same payload and active: false, status: "interrupted". That covers graceful restarts and hard kills alike. Recording interrupted in the cancel handler during close is possible too (close() cancels the loop before closing pub/sub at :2117 and the conversation at :2121), but would not cover a crash.
OpenHands AI triage
The following comments and acceptance criteria were added by the OpenHands AI agent.
Triage
Reproducible, well-scoped Agent Server bug. An active /goal loop cancelled by a graceful shutdown never records a terminal goal status, so after restart the last persisted goal ConversationStateUpdateEvent still reports active: true, status: "running" while no loop runs and /goal/stop is a no-op (there is no task to cancel). This belongs in this repository: goal-loop state and the persisted goal event are owned by the Agent Server (openhands-agent-server/openhands/agent_server/event_service.py); Agent Canvas only renders that status, and the SDK/Agent Server REST/WS contract is canonical here.
Bounded scope: reconcile a stale active: true goal status when a conversation is loaded in EventService.start(), alongside the existing stale-RUNNING execution-status reconcile, so the last goal status becomes active: false, status: "interrupted" with the same objective, iteration, and max_iterations, and the loop stays resumable through POST /goal/resume. This covers both a graceful restart (where close() cancels the loop with _closing=True and the cancel handler deliberately skips the status) and a hard kill (where close() never runs). The mechanism is left to the author; the issue's root-cause analysis is a starting point, not a required design.
Explicit non-goals: no change to goal-loop semantics, judge behavior, iteration/cap accounting, the persisted event schema, or the REST/WS contract; no change to the normal /goal/stop, /interrupt, or user-message-supersede paths, which already record interrupted correctly. Reconciling stale goal state in any other endpoint is out of scope.
Note on the F11.goal-restart-active bullet: it lives in the verify-agent-server feature map, which is still an open PR (#5621) and is not on main. Removing its known-bug marker is only in scope once #5621 has merged; until then the observable server behavior below is the acceptance bar.
Acceptance Criteria
- After a graceful restart (server stopped while execution_status is
runningwith an active loop, then restarted against the same conversations dir), the last persistedgoalConversationStateUpdateEventvalue isactive: false, status: "interrupted", withobjective,iteration, andmax_iterationsidentical to the pre-restartactive: true, status: "running"value. - The same reconciliation happens when a conversation is loaded whose last persisted goal status is
active: truefrom a hard kill (no gracefulclose()), i.e. reconciliation is driven by the loaded persisted state rather than by a shutdown hook; this case is covered by an automated regression test. - After such a restart,
POST /api/conversations/{id}/goal/resumeis admitted (200) and drives the loop to a terminal status (completeorcapped), i.e. the reconciledinterruptedstatus remains resumable;POST /api/conversations/{id}/goal/stopon the restarted, loop-less conversation no longer leaves arunningstatus. - A conversation whose last persisted goal status is already terminal (
completeorcapped) is unchanged on load: no extrainterruptedgoal status is appended. - Existing goal-loop behavior is preserved: the
/goal/stop,/interrupt, unexpected-error, and user-message-supersede paths still recordinterrupted, and the currenttests/agent_server/test_goal_loop.pysuite passes unchanged. - The
goalevent value shape is unchanged (same keys/GoalStatusfields), so existing consumers and the persisted conversation format need no migration. - Once the verify-agent-server feature map is on
main(PR Add verify-agent-server: an executable feature map and control CLI for the Agent Server API #5621), theF11.goal-restart-activemap bullet passes and its known-bug marker is removed; if Add verify-agent-server: an executable feature map and control CLI for the Agent Server API #5621 has not merged, this item is out of scope for this issue.
Activity
enyst commentedon Oct 13, 2026
I'm an AI agent (Claude Code) helping Engel Nyst (@enyst) with project maintenance.
Answers to the triage points, with an independent fix in #5772 (written for comparison with the existing PR, at the maintainer's request):
EventService.start()checks the last persistedgoalstatus next to the existing stale-RUNNINGreconcile; if it is stillactive, it appends oneactive: false, status: "interrupted"status with the sameobjective,iterationandmax_iterations. That covers a graceful restart and a hard kill alike; the cancel handler duringclose()is unchanged.interrupted, so/goal/resumeis admitted and drives the loop to a terminal status (a test covers resume tocompleteafter a reload). Since the stalerunningrow is gone after load,/goal/stopon a loop-less conversation no longer leaves arunningstatus.complete,cappedand an already-interruptedstatus are untouched on load (parametrised test)./goal/stop,/interrupt, unexpected-error and supersede paths are unchanged;tests/agent_server/test_goal_loop.pypasses with only new tests added.GoalStatuspayload (verdict cleared, like every otherinterruptedstatus), so no migration.F11.goal-restart-activemarker is removed in the same PR. Proof:xfailtwice on main,xpasstwice with the fix, thenpasswith the marker removed.No maintainer decision is needed here as far as I can tell.
Generated by Claude Code