Repository navigation
refactor(sdk): change execution_status only through ConversationState.transition - #6620
Conversation
….transition execution_status was assigned at 42 sites across LocalConversation, Agent, ACPAgent, response dispatch and EventService, with no single place that knew which moves are legal. ConversationState.transition(to, reason) now checks the move against a status table, logs the reason at debug level and assigns through the existing autosave hook, so the change is persisted and published as a ConversationStateUpdateEvent as before. The table keeps every move the code makes today (ERROR, PAUSED and RUNNING from any live status; a pause landing mid-step followed by the step's outcome) and rejects the rest, e.g. leaving DELETING or reaching FINISHED from ERROR. A pre-commit hook, scripts/check_execution_status_writes.py, rejects any other execution_status assignment in package code. The five server-side mirror writes (status index records, the webhook payload) carry a "# status-write:" marker. Part of #6456 (step 2, transition half).
This comment has been minimized.
This comment has been minimized.
REST API breakage checks (OpenAPI) — ✅ PASSEDResult: ✅ PASSED |
Coverage Report •
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
The first table rejected moves that real flows make, turning them into ConversationRunError: - sync run() with ACPAgent after a prompt timeout: the agent sets ERROR without raising and the loop retries, so the retried turn's FINISHED (and stuck detection's STUCK) follows ERROR; - condense() or a direct agent.step() on an errored or stuck conversation with a pending finish action or a hook-blocked message writes FINISHED; - pause() landing between the ACP branch's two lock sections makes the final-iteration IDLE follow PAUSED; - a base_state.json left at DELETING made the exception handlers' ERROR and PAUSED writes raise and mask the original error. The table now rejects only moves no code path makes (ERROR -> IDLE, FINISHED -> STUCK, PAUSED -> STUCK) and gives DELETING, which is never set on the state, no special case. Part of #6456.
all-hands-bot
left a comment
There was a problem hiding this comment.
This review was posted by an AI agent (OpenHands).
Scope: The change is entirely inside the Python SDK and Agent Server (conversation status lifecycle, a pre-commit guard, and tests), which this repository owns per AGENTS.md. Linked issue #6456 is open and carries ready-for-dev, so the product/architecture direction is present. Continuing with the technical review.
Verification performed on the current head (ba2daa1):
- I traced every
transition()call site against the_UNREACHED_MOVEStable. The three rejected moves are genuinely unreachable: (1)ERROR -> IDLE-send_message()writes IDLE only from FINISHED/STUCK,reject_pending_actions()only from WAITING_FOR_CONFIRMATION, the sync loop's final-iteration IDLE write is guarded by FINISHED/WAITING_FOR_CONFIRMATION, and the async ACP loop breaks on ERROR/STUCK before its IDLE write; (2)FINISHED -> STUCKand (3)PAUSED -> STUCK- the stuck check runs after the loop'sbreakon PAUSED/STUCK and after the FINISHED stop-hook branch, so_check_stuck_or_nudge()never sees those statuses; the async loop has no stuck check at all. ERROR and PAUSED stay reachable from every status, so the run loop's exception handlers can never themselves raise and mask the original error - verified for theLLMAuthenticationErrorhandler, the genericexcept Exceptionhandler,_emit_run_limit_error, the EventService backstop, and the ACP init-state error path. - The table deliberately allows
ERROR -> FINISHED/STUCK(ACP prompt-timeout retry),PAUSED -> IDLE(pause landing mid-step), andDELETING -> ERROR(foreignbase_state.jsonwriter); each of these is pinned bytest_moves_found_in_review_stay_allowedortest_status_transition_flows.py. - Ran locally: the PR's focused tests pass (28/28 in
test_state_transition.py,test_status_transition_flows.py,test_check_execution_status_writes.py),scripts/check_execution_status_writes.pyexits 0 over the tree,tests/agent_server/test_event_service.pypasses (130, including the five tests adapted to_status_tracking_mock), and the local/remote conversation suites pass (414 + the lifecycle/pause/confirmation/auth-error files). - The five remaining raw writes in
conversation_service.pyupdate the server-side index record and the webhook payload - mirrors, notConversationState- so the# status-write:markers are appropriate. The guard covers plain, annotated, augmented, chained, multiline, andsetattrforms (unit-tested), andtest_package_sources_passgives full-tree CI coverage independent of which files a commit touches. - Backward compatibility is preserved: the table constrains only
transition(); code outside the repo that assignsexecution_statusdirectly keeps working, and no OpenAPI/serialized-event surface changed (Check OpenAPI Schema passed).
CI note (not a finding): the sdk-tests job on this head fails on exactly one test, test_local_conversation_mcp.py::test_failing_mcp_server_keeps_the_other_servers_tools[invalid-entry] (pydantic ValidationError from coerce_mcp_config; 8158 passed, 1 failed). I reproduced the identical failure at the merge base 9f9bd44 (1 failed, 2 passed locally, 3/3 runs deterministic), and this PR touches neither mcp/config.py nor that test file. The param was removed on main in #6613 after this branch was cut. The failure is pre-existing and unrelated to this PR; all other checks on this head pass, including pre-commit with the new hook.
No material correctness, security, compatibility, or acceptance-criterion defect found on the current head.
✅ APPROVED
#6620 added a lifecycle test that relies on run() stepping again after a turn sets ERROR without raising, which is the resend this PR stops (#6645). Keep the test's purpose (ERROR -> RUNNING -> FINISHED goes through transition()) by finishing on the next run() instead. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_013ArwwLKWzfpSTpRccheeWP
HUMAN:
AGENT:
Why
Step 2 of #6456 starts with "a
ConversationState.transition(to, reason)validated against a table and emitting the state-update event; migrate the ~60 assignments and forbid raw assignment with a CI grep." Todayexecution_statusis assigned at 42 places inLocalConversation,Agent,ACPAgent,response_dispatchandEventService. No single place knows which moves are legal, which is the structure behind #884, #2720 and #1511.Summary
ConversationState.transition(to, reason)checks the move against_ALLOWED_TRANSITIONSand raisesInvalidExecutionStatusTransitionErrorfor a move no code path makes. It logsexecution_status <from> -> <to>: <reason>at debug level, then assigns through the existing autosave hook, so the change is persisted and published as aConversationStateUpdateEventexactly as before. Moving to the current status is a no-op.transition()with a short reason. No control flow changed.condense(), a directagent.step(), or the sync loop retrying afterACPAgentreported a prompt timeout as ERROR without raising. So FINISHED, WAITING_FOR_CONFIRMATION and STUCK can follow ERROR.arun()releases the lock for LLM I/O and ACP steps run without it. So PAUSED may be followed by the step's outcome, or by IDLE in the ACP branch.scripts/check_execution_status_writes.py, rejects any otherexecution_statusassignment (plain, annotated, augmented, orsetattr) in the four packages. It runs in the required Pre-commit checks job. The fiveconversation_servicewrites update the status index and the webhook payload, which copy the status rather than beingConversationState, so they carry a# status-write:marker.Issue Number
Part of #6456 (step 2, transition half).
How to Test
Real run against gpt-5.5 through the All-Hands proxy, with
LOG_LEVEL=DEBUGand the state-change callback wired asEventServicewires it. It covers confirmation mode, reject, a sync run, a new message after FINISHED,arun(), and pause then resume. Each step reaches its expected status, and the debug log shows every move with its reason:With an invalid key, the auth-failure path goes running → error through
transition()and raisesConversationRunErroras before.A first, stricter table broke real flows, which a second review reproduced. Each now has a test:
run()withACPAgentafter a prompt timeout. On retry, the turn's FINISHED was ERROR → FINISHED. After three timeouts, stuck detection was ERROR → STUCK. Both maderun()raiseConversationRunError.condense(), or a directagent.step(), on an errored conversation with a pending finish action or a hook-blocked message raised instead of finishing.base_state.jsonleft at "deleting" by a foreign writer made the error handlers raise and mask the original error.test_status_transition_flows.pycovers the first two.test_moves_found_in_review_stay_allowedpins those moves plus the PAUSED → IDLE race in the ACP branch ofarun().Suites run one at a time, as CI runs them:
tests/sdk: 8142 passed.tests/agent_server: 3008 passed. The fivetest_event_service.pytests that mock the state now give the mock atransition()that sets the status.tests/cross: 713 passed, includingtest_conversation_parity.pyandtest_remote_conversation_live_server.py.tests/tools: 1057 passed.The remaining local failures don't touch the status code; none of their tracebacks mention
transitionorexecution_status:git tagunder a global ssh tag-signing config;Video/Screenshots
N/A
Type
Notes
RunControllerinEventService(onerequest_run()owning the permit, task and rerun intent behind idle/admitting/running/draining/closing) is the follow-up PR. It will build ontransition().execution_statusdirectly to arrange fixtures. The hook only scans package code.state.execution_statusdirectly keeps working. Only SDK code is held to the table.🐳 Agent Server images for this PR — GHCR package, pull/run commands, and all pushed tags (click to expand)
• GHCR package: https://github.com/OpenHands/agent-sdk/pkgs/container/agent-server
Variants & Base Images
eclipse-temurin:17-jdkpython-node-runtimepython-node-runtimepython-node-runtimegolang:1.21-bookwormPull (multi-arch manifest)
# Each variant is a multi-arch manifest supporting both amd64 and arm64 docker pull ghcr.io/openhands/agent-server:f535323-pythonRun
All tags pushed for this build
About Multi-Architecture Support
f535323-python) is a multi-arch manifest supporting both amd64 and arm64f535323-python-amd64) are also available if needed