Skip to content

refactor: introduce AgentLoop.from_config() to centralize loop assembly#3708

Merged
Re-bin merged 3 commits into
mainfrom
refactor/provider-factory
May 9, 2026
Merged

refactor: introduce AgentLoop.from_config() to centralize loop assembly#3708
Re-bin merged 3 commits into
mainfrom
refactor/provider-factory

Conversation

@chengyongru
Copy link
Copy Markdown
Collaborator

Summary

Extract duplicated bus/provider/loop initialization from CLI commands (serve, _run_gateway, agent) and Nanobot facade into a single AgentLoop.from_config() classmethod. This is PR 1/4 of the model-preset feature decomposition (cherry-picked from #3358 / #3696).

Changes

  • New: AgentLoop.from_config(config, bus=None, **extra) — creates MessageBus, calls make_provider(config), assembles AgentLoop with all standard config-derived parameters
  • Removed: _make_provider() from cli/commands.py and nanobot.py
  • Removed: Inline 20+ parameter AgentLoop(...) constructor calls in serve(), _run_gateway(), agent()
  • Supports: optional provider, model, context_window_tokens overrides via **extra for callers that need them (e.g. _run_gateway passes snapshot-derived values)

Test Impact

  • Monkeypatch targets updated: _make_providermake_provider at nanobot.providers.factory
  • _FakeAgentLoop fixtures updated with from_config classmethod
  • All 78 CLI + facade tests pass

Scope

Pure refactor — no behavior change, no new features introduced.

Next Steps

  • PR 2: ModelPresetConfig schema + runtime preset switching
  • PR 3: ModelRouter failover with fallback_presets
  • PR 4: Onboard wizard UI + documentation

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
Copy link
Copy Markdown
Collaborator

@Re-bin Re-bin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! @chengyongru

@Re-bin Re-bin merged commit ff6b014 into main May 9, 2026
15 of 16 checks passed
@Re-bin Re-bin deleted the refactor/provider-factory branch May 9, 2026 07:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants