Repository navigation
fix(provider): reload newly configured providers and explain lookup failures - #10895
JosephTian876 wants to merge 1 commit into
Conversation
There was a problem hiding this comment.
Hey - I've found 1 issue
Prompt for AI Agents
Please address the comments from this code review:
## Individual Comments
### Comment 1
<location path="astrbot/core/provider/manager.py" line_range="933-938" />
<code_context>
+ # provider added to the config becomes usable without a restart.
+ for config_entry in self.providers_config:
+ provider_id = config_entry.get("id")
+ if not provider_id or provider_id in self.inst_map:
+ continue
+ # Judge eligibility on the same merged config load_provider() resolves,
+ # so fields inherited from the provider_source count here too.
+ effective_config = self.get_merged_provider_config(config_entry)
+ if not effective_config.get("enable", False):
+ continue
+ if effective_config.get("provider_type", "") == "agent_runner":
</code_context>
<issue_to_address>
**Disabled live provider remains usable**
When a configured provider with a live instance is changed to disabled outside a reload of that same provider, then another provider is reloaded, `_reload_locked()` skips an entry as soon as its ID is in `inst_map`, before checking whether it is now disabled, so the existing instance remains available and lookups continue returning it; callers can use a provider that the configuration disables.
During convergence, check effective enablement for existing instances too and terminate any instance whose effective configuration is disabled.
</issue_to_address>Linked issues
#7425 [Bug]无论如何修改供应商ID,都会有“Fallback chat provider”或是“未找到指定的上下文压缩模型”,随即显示跳过阶段。 · 3 of 3 met
| # | Requirement | Status | Notes |
|---|---|---|---|
| 1 | Configured fallback chat providers should be resolved instead of skipped. | ✅ Met | The reload convergence pass loads eligible configured providers missing from inst_map, allowing fallback lookup to find them. |
| 2 | The configured context-compression model should be resolved instead of skipped. | ✅ Met | Context-compression lookup uses the same provider lookup path, and the added test exercises that path. |
| 3 | The warning should not imply a provider ID changed when the ID is configured. | ✅ Met | Lookup diagnostics distinguish unknown IDs from configured-but-disabled and configured-but-unloaded providers. |
Sourcery assessment
Needs a human reviewer. 1 finding to address first, and a reload can now instantiate enabled providers from the saved configuration, making them available for requests and potentially auto-selecting one if no current provider exists. Prompts sent to a provider's configured endpoint cannot be recalled by reverting the change.
Blocking findings: astrbot/core/provider/manager.py:938
d756694 to
80a1b7e
Compare
|
Rebased onto latest Local Verification
|
80a1b7e to
8bc9fa7
Compare
|
Updated in Verified locally: |
Linked issuesChecked at commit 8bc9fa7 #7425 [Bug]无论如何修改供应商ID,都会有“Fallback chat provider”或是“未找到指定的上下文压缩模型”,随即显示跳过阶段。 · 1 of 1 met
|
Fixes #7425.
Problem
Provider IDs that are fully present in the configuration can be reported as
unavailable for the whole lifetime of the process:
and requests that reference them (Agent Runner fallback chat providers, the
configured context-compression model) are silently skipped until the next
restart. The same message also appears for a provider that the user just
disabled, even though nothing about its ID changed.
Configuration and live instances drift apart
ProviderManagerresolves explicit provider IDs only throughinst_map, themap of live provider instances. The configured provider list
(
providers_config) andinst_mapare two different things, and only startupinitialize()guarantees that every configured entry gets an instance.ProviderManager.reload()re-syncsproviders_configfrom the configurationand terminates instances whose IDs disappeared, but it never instantiates
entries that appeared. So any provider entry that reaches the configuration
without going through
create_provider()(editingcmd_config.json,restoring a backup, a failed adapter import during startup) stays unloaded.
A later save of an unrelated provider runs
reload()again and still does notheal the drift, which is why only a restart helps.
A disabled provider is a second, deliberate case:
load_provider()returnsbefore registration, so the provider correctly has no instance and
get_provider_by_id()correctly returnsNone.Why the message points users in the wrong direction
Context.get_provider_by_id()treated "no live instance" as "unknown ID" andalways logged that the provider or model ID "may have been changed". That
claim is only true for IDs that are absent from the configuration. For a
disabled provider, or a configured provider that failed to load, it asks the
user to hunt for a typo in an ID that is exactly right, and it hides the real
state of the provider.
Changes
Two small, related changes.
ProviderManager.reload()convergesinst_mapwith the configuration.After the existing removal loop, each configured entry that is enabled, is
not an
agent_runnerprovider, and has no live instance is instantiatedinside the manager's existing
resource_lock. Each provider is loaded in itsown
try/exceptwith the same error containmentinitialize()uses, so onebroken provider is logged and cannot abort the reload or block the others.
Eligibility is decided on the entry merged with its
provider_sourceconfig - the same effective configuration
load_provider()resolves - so aprovider that inherits
enableorprovider_typefrom its source is loadedtoo rather than silently skipped. The entry passed to
reload()itself isresolved the same way, so the provider-source upsert path no longer raises
KeyErrorwhen a field is inherited.The method now reads
self.acm.default_confinstead of the module-globalastrbot_config, matching the configuration the manager was constructedfrom and the same object its
create/update/deletemethods operate on.create_provider()syncs that same list after saving. In production bothrefer to the default configuration, so this only removes an accidental
source of divergence.
The whole convergence pass runs under
resource_lock, the same lock thatcreate_provider(),update_provider()anddelete_provider()take, so itcannot race a configuration mutation. This is a real race, not a theoretical
one: the provider-source upsert path calls
reload()without holding anymanager lock, while
delete_provider()takes onlyresource_lock. Withoutserialization,
delete_provider("b")could complete whilereload("a")wassuspended inside
load_provider("b"); the resumed convergence loop thenregistered
bininst_mapafterbhad been removed from theconfiguration. The instance outlived the configuration entry.
update_provider()already holdsresource_lockwhen it reloads, so it nowcalls the private
_reload_locked()body directly and every other callerenters through
reload(). That keeps one lock for both configurationmutation and instance convergence: there is no lock-ordering cycle, and no
path acquires
resource_lockwhile already holding a manager lock. Theformer
reload_lockis removed because, with every reload entered underresource_lock, it could no longer be contended.Context.get_provider_by_id()reports the real reason. On aninst_mapmiss it looks the ID up in the configured provider list. If theID is unknown, the existing wording is preserved verbatim. If the ID is
configured but disabled, the log says it is disabled and cannot be used. If
it is configured but has no instance, the log says it was configured but
not loaded and to check the logs. The method still returns
Nonein everymiss case, so a disabled provider is never silently used.
The diagnostic half matters as much as the loading half: users of #7425 were
told to look for a renamed provider or model, and the only actionable fact -
that their fallback/compression provider was disabled or had failed to load -
was never surfaced. Fixing convergence alone would still leave that misleading
message for genuinely disabled providers.
Verification
Diff scope: 2 production files (
astrbot/core/provider/manager.py+102/-57,astrbot/core/star/context.py+22/-5; total+124/-62, no test files modified in this PR).Eleven local verification scenarios (kept out of this PR diff to keep the change focused on the 2 production files; happy to include compact regression tests if preferred) were verified before/after this fix:
Reload convergence scenarios:
reload()instantiates a provider newly added to the configured list whileinst_mapis stale.reload()skips disabled andagent_runnerentries without creatinginstances, loading only the eligible provider, and without raising.
reload()instantiates a configured entry that inheritsenableandprovider_typeonly from itsprovider_sourceconfig.reload()itself, as the provider-sourceupsert path does, loads it; it previously raised
KeyError: 'enable'.provider_sourceconfig is disabled is still skipped.reload()is logged, does not raiseout of
reload(), and does not prevent the other new provider from loading.delete_provider()for a provider thatreload()is in the middle ofloading leaves that provider absent from the configuration,
providers_configandinst_map. The load is suspended on anasyncio.Event, so the two coroutines deterministically interleave.update_provider()completes and replaces the instance; it holdsresource_lockwhile reloading, so the test times out if the reload pathre-acquires that lock.
Lookup diagnostic scenarios:
get_provider_by_id()on a configured-but-disabled ID returnsNone, logsthat it is disabled, and no longer emits the "may have been changed"
wording.
get_provider_by_id()on a genuinely unknown ID keeps the existing wordingexactly, while a configured-but-unloaded ID gets the truthful message.
_get_compress_provider()and_get_fallback_chat_providers()with a configured-but-disabled ID log the disabled reason for both paths
instead of the "changed" wording.
Seven of the original eight tests fail on
master; the eighth cannot completethere. On
master,reload()re-syncs from the module-global configuration andterminates the instance that is not in it, so the
update_provider()test alsofails there, with
KeyError: 'existing_provider'. The interleaving test hangs onmasterinstead, because the gated convergence load it waits for never starts.The two tests for an entry that inherits
enablefrom its source fail on theintermediate convergence state this branch had before the guard resolved the
merged config: the convergence test leaves the provider unloaded, and the
direct-reload test raises
KeyError: 'enable'. The disabled-source test passesin both states and guards against loading it after the fix.
The interleaving test targets the race introduced by the convergence pass
itself, so it fails on that intermediate state (convergence without the lock,
where
inst_mapkeeps the deleted provider) and passes with the final change.The
update_provider()test fails with aTimeoutErrorif its reload pathre-acquires
resource_lock.Compatibility
unaffected:
reload()finds nothing missing, and lookups hitinst_mapbefore any diagnostic runs.
Suggested merge order
Independent from
#10845and#10846, though all three touchastrbot/core/provider/manager.py. Suggested order: #10845 → #10895 → #10846 (any order works with a minor rebase if needed).Summary by Sourcery
Keep configured provider instances synchronized during reloads and explain provider lookup failures accurately.
Bug Fixes:
Enhancements:
Tests: