Implement agent lifecycle state management:
States: IDLE, RUNNING, PAUSED, STOPPED, DONE, ERROR
Valid Transitions:
- IDLE → RUNNING (start)
- RUNNING → PAUSED (pause), STOPPED (abort), DONE (complete), ERROR (exception)
- PAUSED → RUNNING (resume), STOPPED (abort)
- ERROR is terminal (no exits)
- STOPPED is terminal
- DONE is terminal
Requirements:
transition_to(new_state): Returns True if valid, False if invalid- Record all transitions in
state_historyas{'from': X, 'to': Y} can_execute(): Only True in RUNNING state
Examples
Example 1:
Input:
life = AgentLifecycle(); life.transition_to('RUNNING'); life.stateOutput:
'RUNNING'Explanation: Valid transition from IDLE to RUNNING
Example 2:
Input:
life = AgentLifecycle(); life.transition_to('DONE')Output:
FalseExplanation: Cannot go from IDLE directly to DONE
Starter Code
class AgentLifecycle:
"""
Manage agent lifecycle states: IDLE → RUNNING → PAUSED/STOPPED → DONE
"""
VALID_STATES = ['IDLE', 'RUNNING', 'PAUSED', 'STOPPED', 'DONE', 'ERROR']
def __init__(self):
self.state = 'IDLE'
self.state_history = []
def transition_to(self, new_state):
"""
Transition to new state if valid.
Valid transitions:
IDLE → RUNNING
RUNNING → PAUSED, STOPPED, DONE, ERROR
PAUSED → RUNNING, STOPPED
ERROR → (no transitions)
Returns True if successful, False otherwise
"""
# Your implementation here
pass
def can_execute(self):
"""Check if agent can execute actions (only in RUNNING)"""
# Your implementation here
passPython3
ReadyLines: 1Characters: 0
Ready