Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,13 @@ repos:
files: ^openhands-sdk/.*\.py$
pass_filenames: false
always_run: true
- id: check-execution-status-writes
name: Route execution_status writes through ConversationState.transition
entry: uv run python scripts/check_execution_status_writes.py
language: system
files: ^openhands-(sdk|agent-server|tools|workspace)/.*\.py$
pass_filenames: true
always_run: false
- id: check-import-rules
name: Check import dependency rules
entry: uv
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -879,6 +879,7 @@ async def _conversation_info(
conversation_info = await asyncio.to_thread(
_compose_conversation_info_sync, event_service.stored, state, children
)
# status-write: index mirror
record.execution_status = conversation_info.execution_status
return conversation_info

Expand Down Expand Up @@ -913,6 +914,7 @@ async def _conversation_info(
record.state_signature = signature
record.stored_signature = stored_signature
record.cached_info = conversation_info
# status-write: index mirror
record.execution_status = conversation_info.execution_status
return conversation_info

Expand Down Expand Up @@ -963,6 +965,7 @@ async def _refresh_execution_statuses(self) -> None:
if event_service is not None and event_service.is_open():
# Authoritative: we own it, so disk can only be staler.
state = await event_service.get_state()
# status-write: index mirror
record.execution_status = state.execution_status
# Autosave will invalidate any signature and cached info we hold.
record.state_signature = None
Expand Down Expand Up @@ -992,6 +995,7 @@ async def _refresh_execution_statuses(self) -> None:
# Went live while we were reading disk; memory wins.
continue
if status is not None:
# status-write: index mirror
record.execution_status = status
# A persisted change (or a move to an unreadable state) invalidates
# any cached ConversationInfo derived from the previous snapshot.
Expand Down Expand Up @@ -1883,6 +1887,7 @@ async def delete_conversation(self, conversation_id: UUID) -> bool:
conversation_info = _compose_webhook_conversation_info(
event_service.stored, state
)
# status-write: webhook payload
conversation_info.execution_status = (
ConversationExecutionStatus.DELETING
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -816,7 +816,9 @@ def _mark_error_status_sync(self) -> None:
return
with self._conversation._state as state:
if state.execution_status != ConversationExecutionStatus.ERROR:
state.execution_status = ConversationExecutionStatus.ERROR
state.transition(
ConversationExecutionStatus.ERROR, "run failed outside the run loop"
)

def _publish_error_event_sync(self, exc: BaseException) -> None:
"""Emit a ConversationErrorEvent so the UI sees the failure detail.
Expand Down Expand Up @@ -1456,7 +1458,9 @@ def _token_streaming_callback(chunk: LLMStreamChunk | str) -> None:
# should be surfaced back to the agent.
state = self._conversation.state
if state.execution_status == ConversationExecutionStatus.RUNNING:
state.execution_status = ConversationExecutionStatus.ERROR
state.transition(
ConversationExecutionStatus.ERROR, "stale running status on load"
)
# Crash recovery scans the full log, not the active branch: the
# process may have died between writing an event file and persisting
# the advanced HEAD, so the leaf can lag the on-disk events. (Remote
Expand Down
18 changes: 12 additions & 6 deletions openhands-sdk/openhands/sdk/agent/acp_agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -2551,7 +2551,9 @@ def init_state(
# exception, which still propagates to preserve the existing
# cleanup/re-raise contract that run()/arun() rely on.
try:
state.execution_status = ConversationExecutionStatus.ERROR
state.transition(
ConversationExecutionStatus.ERROR, "ACP agent failed to start"
)
on_event(
ConversationErrorEvent(
source="agent",
Expand Down Expand Up @@ -4134,7 +4136,7 @@ def _finalize_successful_turn(
tool_call_id=tc_id,
)
)
state.execution_status = ConversationExecutionStatus.FINISHED
state.transition(ConversationExecutionStatus.FINISHED, "ACP turn finished")

def _emit_turn_timeout(
self,
Expand Down Expand Up @@ -4168,7 +4170,7 @@ def _emit_turn_timeout(
# Close any tool cards left in flight from the timed-out attempt.
self._cancel_inflight_tool_calls()
on_event(MessageEvent(source="agent", llm_message=error_message))
state.execution_status = ConversationExecutionStatus.ERROR
state.transition(ConversationExecutionStatus.ERROR, "ACP prompt timed out")

def _emit_turn_error(
self,
Expand Down Expand Up @@ -4213,7 +4215,7 @@ def _emit_turn_error(
detail=error_detail,
)
)
state.execution_status = ConversationExecutionStatus.ERROR
state.transition(ConversationExecutionStatus.ERROR, "ACP turn failed")

def _finalize_successful_turn_guarded(
self,
Expand Down Expand Up @@ -4319,7 +4321,9 @@ def _step(
break
if prompt_blocks is None:
logger.warning("No user message found; finishing conversation")
state.execution_status = ConversationExecutionStatus.FINISHED
state.transition(
ConversationExecutionStatus.FINISHED, "no user message to prompt"
)
return

self._reset_client_for_turn(
Expand Down Expand Up @@ -4497,7 +4501,9 @@ async def _astep(
break
if prompt_blocks is None:
logger.warning("No user message found; finishing conversation")
state.execution_status = ConversationExecutionStatus.FINISHED
state.transition(
ConversationExecutionStatus.FINISHED, "no user message to prompt"
)
return

self._reset_client_for_turn(
Expand Down
25 changes: 18 additions & 7 deletions openhands-sdk/openhands/sdk/agent/agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -664,7 +664,7 @@ def get_dynamic_context(self, state: ConversationState) -> str | None:

@staticmethod
def _mark_conversation_finished(state: ConversationState) -> None:
state.execution_status = ConversationExecutionStatus.FINISHED
state.transition(ConversationExecutionStatus.FINISHED, "agent finished")

def _execute_actions(
self,
Expand Down Expand Up @@ -772,7 +772,9 @@ def _step(
reason = state.pop_blocked_message(state.last_user_message_id)
if reason is not None:
logger.info(f"User message blocked by hook: {reason}")
state.execution_status = ConversationExecutionStatus.FINISHED
state.transition(
ConversationExecutionStatus.FINISHED, "user message blocked by hook"
)
return
elif state.blocked_messages:
logger.debug(
Expand Down Expand Up @@ -822,7 +824,10 @@ def _step(
llm_message=_non_multimodal_image_message(self.llm.model),
)
)
state.execution_status = ConversationExecutionStatus.FINISHED
state.transition(
ConversationExecutionStatus.FINISHED,
"model cannot read the attached images",
)
return

logger.debug(
Expand Down Expand Up @@ -983,7 +988,9 @@ async def _astep(
reason = state.pop_blocked_message(state.last_user_message_id)
if reason is not None:
logger.info(f"User message blocked by hook: {reason}")
state.execution_status = ConversationExecutionStatus.FINISHED
state.transition(
ConversationExecutionStatus.FINISHED, "user message blocked by hook"
)
return
elif state.blocked_messages:
logger.debug(
Expand Down Expand Up @@ -1030,7 +1037,10 @@ async def _astep(
llm_message=_non_multimodal_image_message(self.llm.model),
)
)
state.execution_status = ConversationExecutionStatus.FINISHED
state.transition(
ConversationExecutionStatus.FINISHED,
"model cannot read the attached images",
)
return

logger.debug(
Expand Down Expand Up @@ -1238,8 +1248,9 @@ def _set_confirmation_status(
state: ConversationState, risks: list[risk.SecurityRisk]
) -> bool:
if any(state.confirmation_policy.should_confirm(risk) for risk in risks):
state.execution_status = (
ConversationExecutionStatus.WAITING_FOR_CONFIRMATION
state.transition(
ConversationExecutionStatus.WAITING_FOR_CONFIRMATION,
"action needs user confirmation",
)
return True

Expand Down
4 changes: 3 additions & 1 deletion openhands-sdk/openhands/sdk/agent/response_dispatch.py
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,9 @@ def _handle_content_response(
)
self._maybe_emit_vllm_tokens(llm_response, on_event)
logger.debug("LLM produced a message response - awaits user input")
state.execution_status = ConversationExecutionStatus.FINISHED
state.transition(
ConversationExecutionStatus.FINISHED, "agent replied with a message"
)

def _handle_no_content_response(
self,
Expand Down
Loading
Loading