From 7700a2ae1761bc6785fe661cb853d15b6647e02a Mon Sep 17 00:00:00 2001 From: PrimeBuild-pc Date: Wed, 14 Oct 2026 17:08:41 +0200 Subject: [PATCH] fix: one save path for a saved rule's cores The rules editor's core mask went through a new update path that reported success without saving when no affinity payload was built, and kept a legacy mask next to a CPU selection. Merge it with the old single-purpose method (now unused), keep its checks, and localize the undo message. --- Locales/de-DE.xaml | 2 +- Locales/en-US.xaml | 2 +- Locales/es-ES.xaml | 2 +- Locales/fr-FR.xaml | 2 +- Locales/it-IT.xaml | 2 +- Locales/ru-RU.xaml | 2 +- Locales/zh-CN.xaml | 2 +- Services/ProcessRuleCreationService.cs | 79 ++----------------- .../ProcessRuleCreationServiceTests.cs | 12 +-- 9 files changed, 20 insertions(+), 85 deletions(-) diff --git a/Locales/de-DE.xaml b/Locales/de-DE.xaml index 755ccf6..c4e1b78 100644 --- a/Locales/de-DE.xaml +++ b/Locales/de-DE.xaml @@ -247,7 +247,6 @@ Löschen Regel bearbeiten Kerne - Kerne aus dieser Maske übernehmen CPU-Priorität Speicherpriorität E/A-Priorität @@ -776,4 +775,5 @@ Letzte Prioritätsänderung rückgängig machen Prozesse angezeigt Nicht gespeicherte Änderungen + Vorherige Priorität wiederhergestellt. diff --git a/Locales/en-US.xaml b/Locales/en-US.xaml index 36a7e97..1158a7e 100644 --- a/Locales/en-US.xaml +++ b/Locales/en-US.xaml @@ -247,7 +247,6 @@ Delete Edit rule Cores - Set cores from this mask CPU priority Memory priority I/O priority @@ -776,4 +775,5 @@ Undo last priority change processes shown Unsaved changes + Previous priority restored. diff --git a/Locales/es-ES.xaml b/Locales/es-ES.xaml index d6c5226..589a17f 100644 --- a/Locales/es-ES.xaml +++ b/Locales/es-ES.xaml @@ -247,7 +247,6 @@ Eliminar Editar regla Núcleos - Establecer núcleos desde esta máscara Prioridad de CPU Prioridad de memoria Prioridad de E/S @@ -776,4 +775,5 @@ Deshacer el último cambio de prioridad procesos mostrados Cambios sin guardar + Prioridad anterior restaurada. diff --git a/Locales/fr-FR.xaml b/Locales/fr-FR.xaml index 4471838..6083a0e 100644 --- a/Locales/fr-FR.xaml +++ b/Locales/fr-FR.xaml @@ -247,7 +247,6 @@ Supprimer Modifier la règle Cœurs - Définir les cœurs depuis ce masque Priorité CPU Priorité mémoire Priorité E/S @@ -776,4 +775,5 @@ Annuler la dernière modification de priorité processus affichés Modifications non enregistrées + Priorité précédente restaurée. diff --git a/Locales/it-IT.xaml b/Locales/it-IT.xaml index 08952e8..10501a1 100644 --- a/Locales/it-IT.xaml +++ b/Locales/it-IT.xaml @@ -247,7 +247,6 @@ Elimina Modifica regola Core - Imposta i core da questa maschera Priorità CPU Priorità memoria Priorità I/O @@ -776,4 +775,5 @@ Annulla ultima modifica di priorità processi visibili Modifiche non salvate + Priorità precedente ripristinata. diff --git a/Locales/ru-RU.xaml b/Locales/ru-RU.xaml index 6f72c3b..c3a38c9 100644 --- a/Locales/ru-RU.xaml +++ b/Locales/ru-RU.xaml @@ -247,7 +247,6 @@ Удалить Изменить правило Ядра - Задать ядра из этой маски Приоритет ЦП Приоритет памяти Приоритет ввода-вывода @@ -776,4 +775,5 @@ Отменить последнее изменение приоритета процессов показано Несохранённые изменения + Предыдущий приоритет восстановлен. diff --git a/Locales/zh-CN.xaml b/Locales/zh-CN.xaml index 82322b3..3217dc2 100644 --- a/Locales/zh-CN.xaml +++ b/Locales/zh-CN.xaml @@ -247,7 +247,6 @@ 删除 编辑规则 核心 - 从此掩码设置核心 CPU 优先级 内存优先级 I/O 优先级 @@ -776,4 +775,5 @@ 撤销上次优先级更改 个进程已显示 未保存的更改 + 已恢复之前的优先级。 diff --git a/Services/ProcessRuleCreationService.cs b/Services/ProcessRuleCreationService.cs index 29c499a..facdd8f 100644 --- a/Services/ProcessRuleCreationService.cs +++ b/Services/ProcessRuleCreationService.cs @@ -40,11 +40,6 @@ namespace ThreadPilot.Services IReadOnlyList? coreSelection, CancellationToken cancellationToken = default); - Task UpdateRuleCoreSelectionAsync( - string ruleId, - IReadOnlyList coreSelection, - CancellationToken cancellationToken = default); - Task> GetRulesNeedingTopologyReviewAsync( CancellationToken cancellationToken = default); } @@ -256,15 +251,20 @@ namespace ThreadPilot.Services if (coreSelection != null) { var affinity = await this.BuildAffinityPayloadFromCoreSelectionAsync(coreSelection, "Saved rule editor", cancellationToken).ConfigureAwait(false); - if (!affinity.Success || affinity.Payload == null) + if (!affinity.Success) { return affinity; } + if (affinity.Payload == null) + { + return ProcessRuleCreationResult.Failed("NoActionableRulePayload", NoCurrentSettingsMessage); + } + updated = updated with { CpuSelection = affinity.Payload.CpuSelection, - LegacyAffinityMask = affinity.Payload.LegacyAffinityMask, + LegacyAffinityMask = HasSelectionPayload(affinity.Payload.CpuSelection) ? null : affinity.Payload.LegacyAffinityMask, ApplyAffinityOnStart = true, }; } @@ -288,71 +288,6 @@ namespace ThreadPilot.Services }; } - // Setting a rule's cores without the process being around to select. The bits come from a - // saved CPU mask, and go through the same migration path the Process tab uses, so a rule - // edited here is indistinguishable from one saved there. - public async Task UpdateRuleCoreSelectionAsync( - string ruleId, - IReadOnlyList coreSelection, - CancellationToken cancellationToken = default) - { - ArgumentNullException.ThrowIfNull(coreSelection); - - if (string.IsNullOrWhiteSpace(ruleId)) - { - return ProcessRuleCreationResult.Failed("NoSavedRuleToUpdate", NoSavedRuleMessage); - } - - var affinityPayload = await this.BuildAffinityPayloadFromCoreSelectionAsync( - coreSelection, - "Set from a saved CPU mask in the Rules tab", - cancellationToken).ConfigureAwait(false); - if (!affinityPayload.Success) - { - return affinityPayload; - } - - if (affinityPayload.Payload == null) - { - return ProcessRuleCreationResult.Failed("NoActionableRulePayload", NoCurrentSettingsMessage); - } - - var rules = (await this.ruleStore.LoadAsync().ConfigureAwait(false)).ToList(); - var existingIndex = rules.FindIndex(candidate => string.Equals(candidate.Id, ruleId, StringComparison.Ordinal)); - if (existingIndex < 0) - { - return ProcessRuleCreationResult.Failed("NoSavedRuleToUpdate", NoSavedRuleMessage); - } - - var selection = affinityPayload.Payload.CpuSelection; - var existing = rules[existingIndex]; - var updated = existing with - { - CpuSelection = selection, - LegacyAffinityMask = HasSelectionPayload(selection) ? null : affinityPayload.Payload.LegacyAffinityMask, - ApplyAffinityOnStart = true, - UpdatedAt = DateTime.UtcNow, - }; - - rules[existingIndex] = updated; - - cancellationToken.ThrowIfCancellationRequested(); - await this.ruleStore.SaveAsync(rules).ConfigureAwait(false); - - var processName = string.IsNullOrWhiteSpace(updated.ProcessName) - ? "process" - : updated.ProcessName.Trim(); - this.logger.LogInformation("Updated the CPU selection of saved rule {RuleId} for {ProcessName}", updated.Id, processName); - - return new ProcessRuleCreationResult - { - Success = true, - Updated = true, - Rule = updated, - UserMessage = $"Updated the cores of the saved rule for {processName}.", - }; - } - // Nothing checked saved rules against the machine they are running on. A rule that pins // cores which no longer exist does not announce itself: it fails at apply time, inside a // path that reports it as the process having exited. Ask the question where the rules are diff --git a/Tests/ThreadPilot.Core.Tests/ProcessRuleCreationServiceTests.cs b/Tests/ThreadPilot.Core.Tests/ProcessRuleCreationServiceTests.cs index 4056e6d..9cfdf21 100644 --- a/Tests/ThreadPilot.Core.Tests/ProcessRuleCreationServiceTests.cs +++ b/Tests/ThreadPilot.Core.Tests/ProcessRuleCreationServiceTests.cs @@ -645,7 +645,7 @@ namespace ThreadPilot.Core.Tests } [Fact] - public async Task UpdateRuleCoreSelectionAsync_SetsTheCoresWithoutTheProcessRunning() + public async Task UpdateRuleWithCoreSelectionAsync_SetsTheCoresWithoutTheProcessRunning() { var existing = new PersistentProcessRule { @@ -663,7 +663,7 @@ namespace ThreadPilot.Core.Tests ])); var service = CreateService(store, topologyProvider); - var result = await service.UpdateRuleCoreSelectionAsync("rule-1", [false, true]); + var result = await service.UpdateRuleWithCoreSelectionAsync(existing, [false, true]); Assert.True(result.Success); var rule = Assert.Single(store.SavedRules); @@ -674,25 +674,25 @@ namespace ThreadPilot.Core.Tests } [Fact] - public async Task UpdateRuleCoreSelectionAsync_RefusesASelectionWithNoCores() + public async Task UpdateRuleWithCoreSelectionAsync_RefusesASelectionWithNoCores() { var existing = new PersistentProcessRule { Id = "rule-1", ProcessName = "cs2", IsEnabled = true }; var store = new CapturingRuleStore([existing]); var service = CreateService(store); - var result = await service.UpdateRuleCoreSelectionAsync("rule-1", [false, false]); + var result = await service.UpdateRuleWithCoreSelectionAsync(existing, [false, false]); Assert.False(result.Success); Assert.Empty(store.SavedRules); } [Fact] - public async Task UpdateRuleCoreSelectionAsync_ReportsAControlledFailureForAnUnknownRule() + public async Task UpdateRuleWithCoreSelectionAsync_ReportsAControlledFailureForAnUnknownRule() { var store = new CapturingRuleStore([]); var service = CreateService(store); - var result = await service.UpdateRuleCoreSelectionAsync("missing", [true, false]); + var result = await service.UpdateRuleWithCoreSelectionAsync(new PersistentProcessRule { Id = "missing", ProcessName = "cs2" }, [true, false]); Assert.False(result.Success); Assert.Equal("NoSavedRuleToUpdate", result.ErrorCode);