refactor(loop): convert _process_message to functional state machine#3715
Merged
Conversation
- Extract TurnState enum and TurnContext dataclass - Extract state handlers: _state_restore, _state_compact, _state_command, _state_build, _state_run, _state_save, _state_respond - Extract _process_system_message for system message short-circuit - Driver loop uses getattr dispatch over explicit state transitions - Preserve all existing behavior (794 tests passing)
- Fix _assemble_outbound on_stream type annotation (Callable[[str], Awaitable[None]] | None) - Use last_msg consistently in _state_save instead of re-indexing - Remove dead fallback in _state_respond (guaranteed non-None by _state_save) - Change pending_summary type from Any to str | None - Make session optional in TurnContext to avoid redundant fetch - Add defensive dispatch with RuntimeError for missing handlers
- State handlers now return event strings ('ok', 'dispatch', 'shortcut')
- Driver loop uses _TRANSITIONS lookup table: (state, event) -> next_state
- State graph is centralized and visible at a glance
- Added StateTraceEntry to record per-state timing and events
- Driver loop logs state duration + event at debug level
- Exception paths are traced with error field for observability
Collaborator
Author
|
- TurnContext now carries a turn_id (session_key:time_ns) - All state transition debug logs include [turn_id] prefix - RuntimeError messages also include turn_id for observability
This was referenced May 10, 2026
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
Refactor the 300-line
_process_messagemethod into an explicit functional state machine to improve readability and maintainability.Changes
RESTORE → COMPACT → COMMAND → BUILD → RUN → SAVE → RESPONDTurnStateenum andTurnContextdataclass to carry cross-state data_process_system_messagemethodgetattrdispatch (while ctx.state != DONE)Verification
pytest tests/agent/— 794 passedruff check nanobot/agent/loop.py— clean