Skip to content

fix: warn when a configured provider is silently substituted - #10846

Open
JosephTian876 wants to merge 1 commit into
AstrBotDevs:masterfrom
JosephTian876:fix/silent-provider-fallback
Open

JosephTian876 wants to merge 1 commit into
AstrBotDevs:masterfrom
JosephTian876:fix/silent-provider-fallback

Conversation

@JosephTian876

@JosephTian876 JosephTian876 commented Oct 29, 2026 •

Copy link
Copy Markdown
Contributor

Modifications / 改动点

ProviderManager._resolve_using_provider() substitutes the first loaded provider whenever the configured provider ID cannot be resolved — and reports nothing. The warning written for this case sits after the substitution, so it can only fire when no provider exists at all:

# astrbot/core/provider/manager.py (master, abf683071)
provider = self.inst_map.get(provider_id) if provider_id else None
if not provider:
    ...
    provider = self.provider_insts[0] if self.provider_insts else None   # 277 — substitute
...
if not provider and provider_id:                                        # 309 — never true now
    logger.warning(f"Provider {provider_id} was not found. ...")

The one situation the user can do nothing about is reported; the situation that actually needs reporting ("the provider you configured is unavailable, so a different one answered") is silent. The substitute is picked by config array order, not capability, so a vision-only model can answer a plain-text request with nothing in the log to point at it. The same shape covered chat, STT and TTS.

This PR does not change which provider is returned. It adds the missing report, once per (provider_type, configured_id, fallback_id) — so a persistent condition cannot flood the log — and clears that record on reload() so a recurrence is reported again.

Rebased onto abf683071, which moved the body into _resolve_using_provider() and added a ProviderType.DECISION branch. Adaptations: the chat provider is now read from agent_runner.config.model.provider_id instead of provider_settings.default_provider_id; decision providers carry no configured ID, so they are correctly unaffected.

  • This is NOT a breaking change. / 这不是一个破坏性变更。

Screenshots or Test Results / 运行截图或测试结果

Diff scope: 1 production file (astrbot/core/provider/manager.py, +38/-0; no test files modified in this PR).

Local verification (12 standalone cases covering chat/STT/TTS fallback warnings, deduplication, reload() reset, and quiet/normal cases — 8 failed on master, 12 passed with this branch; test file omitted from this PR diff to keep the change focused on the 38-line fix, happy to add compact tests if preferred). Surrounding existing test suites (test_decision_provider.py, test_process_stage_images.py, unit/test_astr_main_agent.py, unit/test_core_lifecycle.py, unit/test_kb_manager_resilience.py, unit/test_knowledge_base_service_contract.py, unit/test_provider_stats.py, unit/test_conversation_restart.py, unit/test_group_chat_context_wiring.py, unit/test_star_context.py) and ruff check / ruff format --check all pass cleanly.

Checklist / 检查清单
  • 😊 No new feature, so nothing to discuss with the authors — this is a logging bug fix.
  • 👀 Changes are tested, and the evidence is above (before/after on the same commit).
  • 📚 No WebUI navigation, page structure or label changes, so docs/zh / docs/en need no update.
  • 🤓 No new dependencies.
  • 😮 No malicious code.

Supersedes #9484

#9484 was closed on 2026-10-12 by the staleness sweep with an invitation to rebase and reopen, not on its merits. GitHub refuses to reopen it: PATCH /pulls/9484 {"state": "open"} returns HTTP 422 state cannot be changed. The fix/silent-provider-fallback branch was force-pushed or recreated. This PR is the same change rebased onto current master, so it supersedes #9484; the original description and discussion stay there.

Suggested merge order

Independent from #10845 and #10895, though all three touch astrbot/core/provider/manager.py. Suggested order: #10845 → #10895 → #10846 (any order works with a minor rebase if needed).

Summary by Sourcery

Report silent provider substitutions while preserving the existing fallback behavior.

Bug Fixes:

  • Warn when a configured chat, speech-to-text, or text-to-speech provider is unavailable and a different provider is substituted.

Enhancements:

  • Deduplicate fallback warnings for each provider substitution and reset them when the affected provider is terminated.

Tests:

  • Add coverage for provider fallback warning behavior and warning suppression.

@sourcery-ai sourcery-ai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Hey - I've found 2 issues

Prompt for AI Agents
Please address the comments from this code review:

## Individual Comments

### Comment 1
<location path="tests/unit/test_provider_fallback_warning.py" line_range="50" />
<code_context>
+    mgr.tts_provider_insts = []
+    mgr.decision_provider_insts = []
+    mgr.curr_decision_provider_inst = None
+    mgr._fallback_warned = set()
+    mgr.acm = MagicMock()
+    return mgr
</code_context>
<issue_to_address>
**Constructor failure is masked**

When `ProviderManager.__init__` omits initialization of `_fallback_warned`, when `ProviderManager.__init__` stops initializing `_fallback_warned`, the warning tests still pass because the `manager` fixture assigns it directly; a real manager then raises `AttributeError` on fallback lookup instead of warning.

Exercise the real constructor in at least one warning test instead of supplying `_fallback_warned` in the fixture.
</issue_to_address>

### Comment 2
<location path="astrbot/core/provider/manager.py" line_range="398-406" />
<code_context>
+        if key in self._fallback_warned:
+            return
+        self._fallback_warned.add(key)
+        logger.warning(
+            "Configured %s provider `%s` is not available; falling back to `%s`. "
+            "This can happen while providers are still loading at startup, or "
+            "when the configured provider was renamed, disabled or removed. "
+            "The fallback may behave differently from the provider you selected.",
+            provider_type.value,
+            configured_id,
+            fallback_id,
+        )
+
     async def initialize(self) -> None:
</code_context>
<issue_to_address>
**Fallback logging is offloaded**

When a configured provider is substituted, the configured-provider fallback warning and its deduplication are handled in `_warn_provider_fallback` rather than directly in `_resolve_using_provider`, contrary to the repository rule for logging and edge-case handling.

Handle the fallback warning and deduplication directly in `_resolve_using_provider`.
</issue_to_address>

Sourcery assessment

Approval pending. 1 finding to address first.

Blocking findings: tests/unit/test_provider_fallback_warning.py:50


Sourcery is free for open source - if you like our reviews please consider sharing them ✨

Comment thread astrbot/core/provider/manager.py Resolved

@sourcery-ai sourcery-ai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Sourcery assessment

Approved.

@JosephTian876

Copy link
Copy Markdown
Contributor Author

Since the last review:

  • The substitution warning and its deduplication moved out of the _warn_provider_fallback helper and inline into _resolve_using_provider.
  • The dedup record is keyed by (provider_type, configured_id, fallback_id), and terminate_provider drops only records that name the provider being removed, so deleting an unrelated provider keeps deduping an unchanged substitution.
  • The warning tests build the manager through the real constructor, so a missing _fallback_warned initialisation fails the suite.

Tests: tests/unit/test_provider_fallback_warning.py has 15 test functions covering chat, STT and TTS substitution, the quiet happy path, per-substitution dedup, the unidentifiable fallback reported as unknown, reload and delete re-arming, and unrelated teardown.

Known limitation: the warning is emitted when a provider is resolved, so a configured substitution that nothing ever asks for produces no log line.

@Soulter Soulter moved this to In Progress in AstrBot Roadmap Nov 2, 2026
@JosephTian876

Copy link
Copy Markdown
Contributor Author

Rebased onto latest master (feb3e82) and adapted to the unified session config schema from #11097 (agent.runtime and _resolve_using_provider signature); all review fixes and tests are preserved.

@JosephTian876
JosephTian876 force-pushed the fix/silent-provider-fallback branch from 506af21 to a53c736 Compare November 21, 2026 21:30
@codecov-commenter

Copy link
Copy Markdown

⚠️ Please install the 'codecov app svg image' to ensure uploads and comments are reliably processed by Codecov.

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

@JosephTian876
JosephTian876 force-pushed the fix/silent-provider-fallback branch from a53c736 to 00b6bd5 Compare November 30, 2026 04:55
@JosephTian876

Copy link
Copy Markdown
Contributor Author

Rebased onto latest master (a084409) and trimmed newly added test files per .github/copilot-instructions.md to keep the diff focused on production changes.

Local Verification

  • Pre-trim regression tests: 16 passed (test_provider_fallback_warning.py)
  • Existing upstream test suite: 47 passed (test_star_context.py)
  • ruff check and ruff format --check: passed (0 issues)

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: In Progress

Development

Successfully merging this pull request may close these issues.

2 participants