refactor: complete Loop struct decomposition (P0 #1-#2, P1 #4, P1 #6) - #131
Merged
Conversation
… (P0 #1) Split the ~70-field Loop struct into: - LoopConfig: 32 immutable config fields (Model, MaxIterations, ContextWindow, etc.) - LoopState: 14 mutable runtime fields (Messages, TotalTokens, etc.) - Loop: ~28 dependency/internals (Provider, Registry, CtxMgr, Persister, etc.) Renamed options.LoopConfig -> AgentConfig and WithLoopConfig -> WithAgentConfig to avoid collision with the new LoopConfig sub-struct type. Addresses ARCHITECTURE-REVIEW.md P0 #1 'Refactor Loop struct into composed types.'
Extracted 7 focused methods from the ~320-line runMiddleware: - publishDone / teardown: deferred cleanup (broker, persister, hooks) - initMessages: conversation initialization and session-start hooks - buildTurnRequest: PrepareStep middleware + request construction + MaxTurns - guardContextBeforeCall: pre-flight compaction + payload guard - recordTurnSpanAttrs: OTel span attribute population - executeToolPhase: tool truncation, execution, conflict detection, PostTool runMiddleware reduced from ~320 lines to ~180 lines with clear delegation.
Renamed across all packages:
- MaxIterations -> MaxLoopCycles (hard limit on total loop cycles)
- MaxTurns -> MaxToolTurns (when tools are stripped)
- WrapUpAhead -> WrapUpThreshold (wrap-up notice threshold)
- WrapUpTurns -> WrapUpThreshold (config yaml alias)
- injectWrapUp -> injectWrapUpNotice
YAML tags preserved for backward compat ('max_iterations', 'max_turns',
'wrap_up_turns' remain unchanged).
Touches: agent, config, pipeline, tools, subagent, cmd/yaah packages.
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.
Implements the Loop struct decomposition plan from \docs/ARCHITECTURE-REVIEW.md.
Completed items
Not done
unMiddleware\ still operates on shared \Loop\ state
Validation