| Sean McCullough | 96b60dd | 2025-04-30 09:49:10 -0700 | [diff] [blame] | 1 | # Agent State Machine Diagram |
| 2 | |
| 3 | ```mermaid |
| 4 | stateDiagram-v2 |
| 5 | [*] --> StateReady |
| 6 | |
| 7 | StateReady --> StateWaitingForUserInput |
| 8 | |
| 9 | StateWaitingForUserInput --> StateSendingToLLM |
| 10 | StateWaitingForUserInput --> StateError |
| 11 | |
| 12 | StateSendingToLLM --> StateProcessingLLMResponse |
| 13 | StateSendingToLLM --> StateError |
| 14 | |
| 15 | StateProcessingLLMResponse --> StateEndOfTurn |
| 16 | StateProcessingLLMResponse --> StateToolUseRequested |
| 17 | StateProcessingLLMResponse --> StateError |
| 18 | |
| 19 | StateEndOfTurn --> StateWaitingForUserInput |
| 20 | |
| 21 | StateToolUseRequested --> StateCheckingForCancellation |
| 22 | |
| 23 | StateCheckingForCancellation --> StateRunningTool |
| 24 | StateCheckingForCancellation --> StateCancelled |
| 25 | |
| 26 | StateRunningTool --> StateCheckingGitCommits |
| 27 | StateRunningTool --> StateError |
| 28 | |
| 29 | StateCheckingGitCommits --> StateRunningAutoformatters |
| 30 | StateCheckingGitCommits --> StateCheckingBudget |
| 31 | |
| 32 | StateRunningAutoformatters --> StateCheckingBudget |
| 33 | |
| 34 | StateCheckingBudget --> StateGatheringAdditionalMessages |
| 35 | StateCheckingBudget --> StateBudgetExceeded |
| 36 | |
| 37 | StateGatheringAdditionalMessages --> StateSendingToolResults |
| 38 | StateGatheringAdditionalMessages --> StateError |
| 39 | |
| 40 | StateSendingToolResults --> StateProcessingLLMResponse |
| 41 | StateSendingToolResults --> StateError |
| 42 | |
| 43 | StateError --> StateWaitingForUserInput |
| 44 | StateCancelled --> StateWaitingForUserInput |
| 45 | StateBudgetExceeded --> StateWaitingForUserInput |
| 46 | ``` |
| 47 | |
| 48 | ## State Descriptions |
| 49 | |
| 50 | | State | Description | |
| 51 | |-------|-------------| |
| 52 | | StateReady | Initial state when the agent is initialized and ready to operate | |
| 53 | | StateWaitingForUserInput | Agent is waiting for a user message to start a turn | |
| 54 | | StateSendingToLLM | Agent is sending message(s) to the LLM | |
| 55 | | StateProcessingLLMResponse | Agent is processing a response from the LLM | |
| 56 | | StateEndOfTurn | Processing completed without tool use, turn ends | |
| 57 | | StateToolUseRequested | LLM has requested to use a tool | |
| 58 | | StateCheckingForCancellation | Agent checks if user requested cancellation | |
| 59 | | StateRunningTool | Agent is executing the requested tool | |
| 60 | | StateCheckingGitCommits | Agent checks for new git commits after tool execution | |
| 61 | | StateRunningAutoformatters | Agent runs code formatters on new commits | |
| 62 | | StateCheckingBudget | Agent verifies if budget limits are exceeded | |
| 63 | | StateGatheringAdditionalMessages | Agent collects user messages that arrived during tool execution | |
| 64 | | StateSendingToolResults | Agent sends tool results back to the LLM | |
| 65 | | StateCancelled | Operation was cancelled by the user | |
| 66 | | StateBudgetExceeded | Budget limit was reached | |
| 67 | | StateError | An error occurred during processing | |
| 68 | |
| 69 | ## Implementation Details |
| 70 | |
| 71 | This state machine is implemented in `statemachine.go` and follows the State pattern design. Key features include: |
| 72 | |
| 73 | 1. Explicit state enumeration for all possible states |
| 74 | 2. Validation of state transitions |
| 75 | 3. History tracking for debugging |
| 76 | 4. Event recording for each transition |
| 77 | 5. Timing information for performance analysis |
| 78 | 6. Error state detection |
| 79 | |
| 80 | The `AgentWithStateMachine` in `statemachine_example.go` demonstrates how this state machine could be integrated with the existing Agent implementation. |