9696from .auth import api as auth_api
9797from .control_tasks import (
9898 _CONTROL_TO_POWER_STATE ,
99- _CONTROL_TRANSITION_INITIAL ,
10099 dispatch_control ,
101- perform_control ,
100+ get_control_in_progress_status ,
102101)
103102from .resize_tasks import dispatch_resize
104103from .tasks import task_api
@@ -234,29 +233,34 @@ def _should_update_branch_status(
234233def _adjust_derived_status_for_stuck_creation (
235234 branch : Branch , current : BranchServiceStatus , derived : BranchServiceStatus
236235) -> BranchServiceStatus :
237- if derived != BranchServiceStatus .STOPPED :
236+ if derived not in {BranchServiceStatus .STOPPED , BranchServiceStatus .ERROR }:
237+ return derived
238+ if current not in {BranchServiceStatus .CREATING , BranchServiceStatus .STARTING }:
238239 return derived
239240
240241 status_timestamp = branch .status_updated_at or branch .created_datetime
241242 elapsed = datetime .now (UTC ) - status_timestamp
242243
243- if current == BranchServiceStatus .CREATING and elapsed >= _CREATING_STATUS_ERROR_GRACE_PERIOD :
244- logger .warning (
245- "Branch %s still CREATING after %s with STOPPED services; marking ERROR" ,
246- branch .id ,
247- elapsed ,
248- )
249- return BranchServiceStatus .ERROR
244+ if current == BranchServiceStatus .CREATING :
245+ if derived == BranchServiceStatus .STOPPED and elapsed >= _CREATING_STATUS_ERROR_GRACE_PERIOD :
246+ logger .warning (
247+ "Branch %s still CREATING after %s with STOPPED services; marking ERROR" ,
248+ branch .id ,
249+ elapsed ,
250+ )
251+ return BranchServiceStatus .ERROR
252+ return derived
250253
251- if current == BranchServiceStatus .STARTING and elapsed >= _STARTING_STATUS_ERROR_GRACE_PERIOD :
254+ # current == STARTING
255+ if elapsed >= _STARTING_STATUS_ERROR_GRACE_PERIOD :
252256 logger .warning (
253- "Branch %s still STARTING after %s with STOPPED services ; marking ERROR" ,
257+ "Branch %s still STARTING after %s; marking ERROR" ,
254258 branch .id ,
255259 elapsed ,
256260 )
257261 return BranchServiceStatus .ERROR
258-
259- return derived
262+ # Within grace period: suppress both STOPPED and ERROR — stay STARTING
263+ return current
260264
261265
262266async def refresh_branch_status (branch_id : Identifier ) -> BranchServiceStatus :
@@ -280,10 +284,9 @@ async def refresh_branch_status(branch_id: Identifier) -> BranchServiceStatus:
280284def _active_task_status (branch : Branch ) -> BranchServiceStatus | None :
281285 """Return the in-progress status for the running task, or None if idle."""
282286 if branch .control_task_id is not None :
283- result = perform_control .AsyncResult (str (branch .control_task_id ))
284- action = (result .kwargs or {}).get ("action" )
285- if action in _CONTROL_TRANSITION_INITIAL :
286- return _CONTROL_TRANSITION_INITIAL [action ]
287+ status = get_control_in_progress_status (branch .control_task_id )
288+ if status is not None :
289+ return status
287290 if branch .resize_task_id is not None :
288291 return BranchServiceStatus .RESIZING
289292 return None
0 commit comments