Agent State Machine Diagram

stateDiagram-v2
    [*] --> StateReady
    
    StateReady --> StateWaitingForUserInput
    
    StateWaitingForUserInput --> StateSendingToLLM
    StateWaitingForUserInput --> StateError
    
    StateSendingToLLM --> StateProcessingLLMResponse
    StateSendingToLLM --> StateError
    
    StateProcessingLLMResponse --> StateEndOfTurn
    StateProcessingLLMResponse --> StateToolUseRequested
    StateProcessingLLMResponse --> StateError
    
    StateEndOfTurn --> StateWaitingForUserInput
    
    StateToolUseRequested --> StateCheckingForCancellation
    
    StateCheckingForCancellation --> StateRunningTool
    StateCheckingForCancellation --> StateCancelled
    
    StateRunningTool --> StateCheckingGitCommits
    StateRunningTool --> StateError
    
    StateCheckingGitCommits --> StateRunningAutoformatters
    StateCheckingGitCommits --> StateCheckingBudget
    
    StateRunningAutoformatters --> StateCheckingBudget
    
    StateCheckingBudget --> StateGatheringAdditionalMessages
    StateCheckingBudget --> StateBudgetExceeded
    
    StateGatheringAdditionalMessages --> StateSendingToolResults
    StateGatheringAdditionalMessages --> StateError
    
    StateSendingToolResults --> StateProcessingLLMResponse
    StateSendingToolResults --> StateError
    
    StateError --> StateWaitingForUserInput
    StateCancelled --> StateWaitingForUserInput
    StateBudgetExceeded --> StateWaitingForUserInput

State Descriptions

StateDescription
StateReadyInitial state when the agent is initialized and ready to operate
StateWaitingForUserInputAgent is waiting for a user message to start a turn
StateSendingToLLMAgent is sending message(s) to the LLM
StateProcessingLLMResponseAgent is processing a response from the LLM
StateEndOfTurnProcessing completed without tool use, turn ends
StateToolUseRequestedLLM has requested to use a tool
StateCheckingForCancellationAgent checks if user requested cancellation
StateRunningToolAgent is executing the requested tool
StateCheckingGitCommitsAgent checks for new git commits after tool execution
StateRunningAutoformattersAgent runs code formatters on new commits
StateCheckingBudgetAgent verifies if budget limits are exceeded
StateGatheringAdditionalMessagesAgent collects user messages that arrived during tool execution
StateSendingToolResultsAgent sends tool results back to the LLM
StateCancelledOperation was cancelled by the user
StateBudgetExceededBudget limit was reached
StateErrorAn error occurred during processing

Implementation Details

This state machine is implemented in statemachine.go and follows the State pattern design. Key features include:

  1. Explicit state enumeration for all possible states
  2. Validation of state transitions
  3. History tracking for debugging
  4. Event recording for each transition
  5. Timing information for performance analysis
  6. Error state detection

The AgentWithStateMachine in statemachine_example.go demonstrates how this state machine could be integrated with the existing Agent implementation.