refactor: Replace agent loop with event-driven state machine#379
Merged
refactor: Replace agent loop with event-driven state machine#379
Conversation
Contributor
Author
TODOs
|
ig-semantic-release-bot bot
pushed a commit
that referenced
this pull request
Jan 25, 2026
## [0.99.2](v0.99.1...v0.99.2) (2026-01-25) ### ♻️ Code Refactoring * Replace agent loop with event-driven state machine ([#379](#379)) ([536fdc6](536fdc6))
|
🎉 This PR is included in version 0.99.2 🎉 The release is available on GitHub release Your semantic-release bot 📦🚀 |
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.
Overview
This PR completes the agent state machine refactoring by extracting all state handlers into separate, maintainable files following the State Design Pattern. This improves code organization, readability, and scalability.
Problem Statement
The previous implementation had all state handlers in a single 1400+ line file (
agent_event_driven.go), making it:Solution
Refactored the agent state machine to follow the State Design Pattern:
Architecture Changes
Before:
After:
Key Components
State Handler Interface (
state_handler.go)Handle(event)method all states must implementStateContextprovides access to agent dependenciesConcrete States (
states/*.go)StateHandlerinterfaceEvent-Driven Agent (
agent_event_driven.go)Changes Made
Phase 1: Infrastructure
StateHandlerinterface andStateContextstructstates/package for state implementationsPhase 2: State Extraction
Extracted all 12 states into separate files:
idle.go- Initial state, transitions to CheckingQueueerror.go- Terminal state for errorscancelled.go- Terminal state for user cancellationstopped.go- Terminal state for stopscompleting.go- Finalization and cleanupevaluating_tools.go- Tool approval evaluationstreaming_llm.go- LLM streaming coordinationpost_stream.go- Post-streaming transitionsexecuting_tools.go- Tool execution coordinationchecking_queue.go- Queue processing and completion logicapproving_tools.go- User approval workflowpost_tool_execution.go- Post-tool transitionsPhase 3: Event-Driven Agent Refactoring
stateHandlersmap)registerStateHandlers()methodhandleEvent()to dispatch patternhandle*State()methods (~1000 lines deleted)Phase 4: Testing & Cleanup
Benefits
Code Organization
Maintainability
Testability
StateContextfor state-specific testsScalability
Design Patterns Applied
StateContextinterfaceTesting
go test -race)Migration Impact
Verification
Breaking Changes
None. This is a pure refactoring with no API or behavior changes.
References