feat(agent): add explicit execution state machine#1944
Open
cal-gooo wants to merge 1 commit intostrands-agents:mainfrom
Open
feat(agent): add explicit execution state machine#1944cal-gooo wants to merge 1 commit intostrands-agents:mainfrom
cal-gooo wants to merge 1 commit intostrands-agents:mainfrom
Conversation
Introduces AgentStateMachine and AgentExecutionState to make the agent's implicit lifecycle phases observable and serializable, enabling durable agents and fine-grained hook-based observability. Key additions: - `AgentStateMachine` with validated state transitions and listener callbacks - `AgentExecutionState` enum: IDLE, INITIALIZING, MODEL_CALL, TOOL_EXECUTION, INTERRUPTED, COMPLETED, CANCELLED, ERROR - `CHECKPOINT_STATES` marks safe points for durable agent snapshots (IDLE, INTERRUPTED, COMPLETED) - `AgentStateTransitionEvent` hook fires on every state change, enabling durability checkpoints, metrics, and audit logging - `agent.state_machine.to_dict()` / `from_dict()` for serialization Wired into Agent.stream_async, _run_loop, and event_loop_cycle so all transitions are tracked automatically. Closes strands-agents#1921 Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Closes #1921
This PR introduces a formal
AgentStateMachineto make the agent's implicit execution lifecycle explicit, enabling durable agents, observability hooks, and safe checkpointing.What's added
AgentStateMachine(src/strands/agent/state_machine.py) — tracks lifecycle with validated transitions and synchronous listener callbacksAgentExecutionStateenum —IDLE → INITIALIZING → MODEL_CALL ↔ TOOL_EXECUTION → COMPLETED/INTERRUPTED/CANCELLED/ERROR → IDLECHECKPOINT_STATES— marksIDLE,INTERRUPTED, andCOMPLETEDas safe states to snapshot agent data for durabilityAgentStateTransitionEventhook — fires on every state change so plugins can react (e.g. persist a snapshot when entering a checkpoint state)state_machine.to_dict()/AgentStateMachine.from_dict(data)for restoring state after a process restartHow it's wired
agent.stream_asyncIDLE → INITIALIZINGat start;→ IDLEin finally (skips ifINTERRUPTED)agent._run_loop→ COMPLETED / INTERRUPTED / CANCELLEDfromEventLoopStopEvent.stop_reason;COMPLETED → INITIALIZINGonAfterInvocationEvent.resumeevent_loop_cycle→ MODEL_CALL(normal path) or→ TOOL_EXECUTION(interrupt/pending tool_use path);MODEL_CALL → TOOL_EXECUTIONwhen model returnstool_usetry_transition(ERROR)on unhandled exceptionsUsage
Test plan
python -m pytest tests/strands/agent/ tests/strands/event_loop/ tests/strands/hooks/)INTERRUPTED → INITIALIZING → TOOL_EXECUTION) verified🤖 Generated with Claude Code