Skip to content

refactor: complete Loop struct decomposition (P0 #1-#2, P1 #4, P1 #6) - #131

Merged
buchenberg merged 6 commits into
mainfrom
refactor/complete-ctxmgr-migration
Aug 4, 2026
Merged

refactor: complete Loop struct decomposition (P0 #1-#2, P1 #4, P1 #6)#131
buchenberg merged 6 commits into
mainfrom
refactor/complete-ctxmgr-migration

Conversation

@buchenberg

Copy link
Copy Markdown
Owner

Implements the Loop struct decomposition plan from \docs/ARCHITECTURE-REVIEW.md.

Completed items

# Item Commit
P1 #6 Remove 11 deprecated fields (\DB, \WriteDebouncer, \MsgIdx, \Pruner, \ToolResultMaxLines, \ToolResultMaxBytes, \PruneProtectTokens, \PruneMinReclaim, \PruneMinTurns, \ReasoningProtectTurns, \HookDir) — migrated to \SessionPersister/\ContextManager/\HookEmitter\ e4fa6fd
P0 #1 Split 70-field \Loop\ struct into \LoopConfig\ (32 immutable config) + \LoopState\ (14 mutable runtime) + \Loop\ (28 deps/internals). Renamed \options.LoopConfig\ → \AgentConfig\ 852a227
P0 #2 Decompose \
unMiddleware\ from ~320 lines into 7 focused helpers: \initMessages, \�uildTurnRequest, \guardContextBeforeCall, \
ecordTurnSpanAttrs, \�xecuteToolPhase, \publishDone, \ eardown\ 37fff48
P1 #4 Clarify naming: \MaxIterations\→\MaxLoopCycles, \MaxTurns\→\MaxToolTurns, \WrapUpAhead\→\WrapUpThreshold\ across all packages 2341723

Not done

Validation

  • \go build ./...\ — clean
  • \go test ./...\ — all green
  • \go vet\ — clean
  • \staticcheck\ — clean
  • \gofmt\ — clean

… (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.
@buchenberg
buchenberg merged commit b26e9e9 into main Aug 4, 2026
3 of 4 checks passed
@buchenberg
buchenberg deleted the refactor/complete-ctxmgr-migration branch August 4, 2026 03:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant