AiAgentManager merges config entities in __construct(), causing stale plugin definitions after ai_agent save
## Problem `Drupal\ai_agents\PluginManager\AiAgentManager` merges `ai_agent` config entities into its plugin definitions from its **constructor**, not from `findDefinitions()`: ```php // src/PluginManager/AiAgentManager.php (constructor) parent::__construct(...); $this->mergeAgentConfigurations(); // ← runs ONCE per instance $this->alterInfo('ai_agents_info'); $this->setCacheBackend($cache_backend, 'ai_agents_plugins'); ``` `mergeAgentConfigurations()` does `entityTypeManager->getStorage('ai_agent')->loadMultiple()` and writes the result into `$this->definitions` — bypassing Drupal's normal plugin discovery + cache lifecycle. This means: 1. The merge happens exactly once, the first time the service is resolved in a request. 2. `clearCachedDefinitions()` does **not** re-merge — it only nulls `$this->definitions` and wipes the persistent cache, and the merge isn't part of `findDefinitions()`. 3. Any code that saves or deletes an `ai_agent` entity after the manager has been resolved gets stale state until the container is fully rebuilt. ## How this manifests If anything triggers the function-call plugin manager during an `ai_agent` save — for example, calling `\Drupal::service('plugin.manager.ai.function_calls')->getDefinitions()` from `AiAgent::calculateDependencies()` — `hook_ai_function_call_info_alter()` (in `ai_agents.module`) lazy-instantiates `plugin.manager.ai_agents` for the first time. Its constructor runs `mergeAgentConfigurations()` mid-save, before the in-flight entity is committed to storage. The entity is omitted from the merge, the function-call manager caches that stale result, and both singletons stay poisoned for the rest of the request. Symptom: ``` Drupal\Component\Plugin\Exception\PluginNotFoundException: The "ai_agents::ai_agent::<my_agent_id>" plugin does not exist. ``` …even though the agent was successfully saved moments earlier. A `clearCachedDefinitions()` call on either manager after the save does not recover, because the constructor-only merge never re-runs. ## Why this is a real bug, not just a test quirk Any production code path that: 1. Saves an `ai_agent` config entity at runtime, then 2. Tries to use it as a function-call plugin in the same request …will hit the same staleness. Kernel tests just make it reliably reproducible. ## AI disclosure * [x] AI-Generated: Yes (Used Claude Code to analyze the discovery chain and draft this report.)
issue