From dbc2181ee65789a244e8f9cd791973152bffb15a Mon Sep 17 00:00:00 2001 From: Thorsten Sommer Date: Mon, 16 Feb 2026 19:14:14 +0100 Subject: [PATCH] Fix cold-start enterprise cleanup to preserve configs when servers are unreachable --- .../Layout/MainLayout.razor.cs | 16 ++-- .../Services/EnterpriseEnvironmentService.cs | 86 ++++++++++++++----- 2 files changed, 72 insertions(+), 30 deletions(-) diff --git a/app/MindWork AI Studio/Layout/MainLayout.razor.cs b/app/MindWork AI Studio/Layout/MainLayout.razor.cs index 3561889d..07dfebd2 100644 --- a/app/MindWork AI Studio/Layout/MainLayout.razor.cs +++ b/app/MindWork AI Studio/Layout/MainLayout.razor.cs @@ -215,29 +215,27 @@ public partial class MainLayout : LayoutComponentBase, IMessageBusReceiver, ILan .CheckDeferredMessages(Event.STARTUP_ENTERPRISE_ENVIRONMENT) .Where(env => env != default) .ToList(); - var wereDeferredDownloadsSuccessful = true; + + var failedDeferredConfigIds = new HashSet(); foreach (var env in enterpriseEnvironments) { var wasDownloadSuccessful = await PluginFactory.TryDownloadingConfigPluginAsync(env.ConfigurationId, env.ConfigurationServerUrl); if (!wasDownloadSuccessful) { - wereDeferredDownloadsSuccessful = false; + failedDeferredConfigIds.Add(env.ConfigurationId); this.Logger.LogWarning("Failed to download deferred enterprise configuration '{ConfigId}' during startup. Keeping managed plugins unchanged.", env.ConfigurationId); - break; } } - if (EnterpriseEnvironmentService.HasValidEnterpriseSnapshot && wereDeferredDownloadsSuccessful) + if (EnterpriseEnvironmentService.HasValidEnterpriseSnapshot) { var activeConfigIds = EnterpriseEnvironmentService.CURRENT_ENVIRONMENTS .Select(env => env.ConfigurationId) .ToHashSet(); + PluginFactory.RemoveUnreferencedManagedConfigurationPlugins(activeConfigIds); - } - else if (!wereDeferredDownloadsSuccessful) - { - // Force a retry on the next enterprise sync cycle. - EnterpriseEnvironmentService.CURRENT_ENVIRONMENTS = []; + if (failedDeferredConfigIds.Count > 0) + this.Logger.LogWarning("Deferred startup updates failed for {FailedCount} enterprise configuration(s). Those configurations were kept unchanged.", failedDeferredConfigIds.Count); } // Initialize the enterprise encryption service for decrypting API keys: diff --git a/app/MindWork AI Studio/Tools/Services/EnterpriseEnvironmentService.cs b/app/MindWork AI Studio/Tools/Services/EnterpriseEnvironmentService.cs index e9bd5a77..badaf5ed 100644 --- a/app/MindWork AI Studio/Tools/Services/EnterpriseEnvironmentService.cs +++ b/app/MindWork AI Studio/Tools/Services/EnterpriseEnvironmentService.cs @@ -53,11 +53,20 @@ public sealed class EnterpriseEnvironmentService(ILogger(); + var reachableEnvironments = new List(); + var failedConfigIds = new HashSet(); + var currentEnvironmentsById = CURRENT_ENVIRONMENTS + .GroupBy(env => env.ConfigurationId) + .ToDictionary(group => group.Key, group => group.Last()); + + var activeFetchedEnvironmentsById = fetchedConfigs + .Where(config => config.IsActive) + .GroupBy(config => config.ConfigurationId) + .ToDictionary(group => group.Key, group => group.Last()); + foreach (var config in fetchedConfigs) { if (!config.IsActive) @@ -69,59 +78,94 @@ public sealed class EnterpriseEnvironmentService(ILogger e.ConfigurationId).ToHashSet(); - var nextIds = nextEnvironments.Select(e => e.ConfigurationId).ToHashSet(); var shouldDeferStartupDownloads = isFirstRun && !PluginFactory.IsInitialized; + var effectiveEnvironmentsById = new Dictionary(); // Process new or changed configs: - foreach (var nextEnv in nextEnvironments) + foreach (var nextEnv in reachableEnvironments) { - var currentEnv = CURRENT_ENVIRONMENTS.FirstOrDefault(e => e.ConfigurationId == nextEnv.ConfigurationId); - if (currentEnv == nextEnv) // Hint: This relies on the record equality to check if anything relevant has changed (e.g. server URL or ETag). + var hasCurrentEnvironment = currentEnvironmentsById.TryGetValue(nextEnv.ConfigurationId, out var currentEnv); + if (hasCurrentEnvironment && currentEnv == nextEnv) // Hint: This relies on the record equality to check if anything relevant has changed (e.g. server URL or ETag). { logger.LogInformation("Enterprise configuration '{ConfigId}' has not changed. No update required.", nextEnv.ConfigurationId); + effectiveEnvironmentsById[nextEnv.ConfigurationId] = nextEnv; continue; } - var isNew = !currentIds.Contains(nextEnv.ConfigurationId); - if(isNew) + if(!hasCurrentEnvironment) logger.LogInformation("Detected new enterprise configuration with ID '{ConfigId}' and server URL '{ServerUrl}'.", nextEnv.ConfigurationId, nextEnv.ConfigurationServerUrl); else logger.LogInformation("Detected change in enterprise configuration with ID '{ConfigId}'. Server URL or ETag has changed.", nextEnv.ConfigurationId); if (shouldDeferStartupDownloads) + { MessageBus.INSTANCE.DeferMessage(null, Event.STARTUP_ENTERPRISE_ENVIRONMENT, nextEnv); + effectiveEnvironmentsById[nextEnv.ConfigurationId] = nextEnv; + } else { var wasDownloadSuccessful = await PluginFactory.TryDownloadingConfigPluginAsync(nextEnv.ConfigurationId, nextEnv.ConfigurationServerUrl); if (!wasDownloadSuccessful) { - logger.LogWarning("Failed to update enterprise configuration '{ConfigId}'. Keeping current plugins unchanged.", nextEnv.ConfigurationId); - return; + failedConfigIds.Add(nextEnv.ConfigurationId); + if (hasCurrentEnvironment) + { + logger.LogWarning("Failed to update enterprise configuration '{ConfigId}'. Keeping the previously active version.", nextEnv.ConfigurationId); + effectiveEnvironmentsById[nextEnv.ConfigurationId] = currentEnv; + } + else + logger.LogWarning("Failed to download the new enterprise configuration '{ConfigId}'. Skipping activation for now.", nextEnv.ConfigurationId); + + continue; } + + effectiveEnvironmentsById[nextEnv.ConfigurationId] = nextEnv; } } + // Retain configurations for all failed IDs. On cold start there might be no + // previous in-memory snapshot yet, so we also keep the current fetched entry + // to protect it from cleanup while the server is unreachable. + foreach (var failedConfigId in failedConfigIds) + { + if (effectiveEnvironmentsById.ContainsKey(failedConfigId)) + continue; + + if (!currentEnvironmentsById.TryGetValue(failedConfigId, out var retainedEnvironment)) + { + if (!activeFetchedEnvironmentsById.TryGetValue(failedConfigId, out retainedEnvironment)) + continue; + + logger.LogWarning("Could not refresh enterprise configuration '{ConfigId}'. Protecting it from cleanup until connectivity is restored.", failedConfigId); + } + else + logger.LogWarning("Could not refresh enterprise configuration '{ConfigId}'. Keeping the previously active version.", failedConfigId); + + effectiveEnvironmentsById[failedConfigId] = retainedEnvironment; + } + + var effectiveEnvironments = effectiveEnvironmentsById.Values.ToList(); + // Cleanup is only allowed after a successful sync cycle: if (PluginFactory.IsInitialized && !shouldDeferStartupDownloads) - PluginFactory.RemoveUnreferencedManagedConfigurationPlugins(nextIds); + PluginFactory.RemoveUnreferencedManagedConfigurationPlugins(effectiveEnvironmentsById.Keys.ToHashSet()); - if (nextEnvironments.Count == 0) + if (effectiveEnvironments.Count == 0) logger.LogInformation("AI Studio runs without any enterprise configurations."); - CURRENT_ENVIRONMENTS = nextEnvironments; + CURRENT_ENVIRONMENTS = effectiveEnvironments; HasValidEnterpriseSnapshot = true; } catch (Exception e)