refactor: introduce AgentLoop.from_config() to centralize loop assembly#3708
Merged
Conversation
Extract duplicated bus/provider/loop initialization from CLI commands (serve, _run_gateway, agent) and Nanobot facade into a single AgentLoop.from_config() classmethod. - Remove _make_provider() from cli/commands.py and nanobot.py - Remove inline provider creation in all three CLI entry points - AgentLoop.from_config() creates MessageBus, calls make_provider(), and assembles AgentLoop with all standard config-derived parameters - Supports **extra overrides for callers that need custom args (e.g. cron_service, session_manager, provider_snapshot_loader) - Update tests to mock make_provider at nanobot.providers.factory and add from_config classmethod to _FakeAgentLoop fixtures This is PR 1/4 of the model-preset feature decomposition.
- Accept optional `provider` kwarg in from_config() to avoid double instantiation in _run_gateway (which already builds provider_snapshot) - Restore try/except ValueError wrappers in serve() and agent() for clean error messages on provider creation failure - Update test: _FakeAgentLoop captures provider from kwargs, restore strong assertion (seen["provider"] is provider)
- Pop model and context_window_tokens from extra kwargs before forwarding to __init__, allowing callers like _run_gateway to pass snapshot-derived values instead of config defaults - _run_gateway now explicitly passes model/context_window_tokens from provider_snapshot to preserve pre-refactor behavior
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
Extract duplicated bus/provider/loop initialization from CLI commands (
serve,_run_gateway,agent) andNanobotfacade into a singleAgentLoop.from_config()classmethod. This is PR 1/4 of the model-preset feature decomposition (cherry-picked from #3358 / #3696).Changes
AgentLoop.from_config(config, bus=None, **extra)— createsMessageBus, callsmake_provider(config), assemblesAgentLoopwith all standard config-derived parameters_make_provider()fromcli/commands.pyandnanobot.pyAgentLoop(...)constructor calls inserve(),_run_gateway(),agent()provider,model,context_window_tokensoverrides via**extrafor callers that need them (e.g._run_gatewaypasses snapshot-derived values)Test Impact
_make_provider→make_provideratnanobot.providers.factory_FakeAgentLoopfixtures updated withfrom_configclassmethodScope
Pure refactor — no behavior change, no new features introduced.
Next Steps
ModelPresetConfigschema + runtime preset switchingModelRouterfailover withfallback_presets