Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions StabilityMatrix.Core/Models/Packages/ComfyUI.cs
Original file line number Diff line number Diff line change
Expand Up @@ -221,10 +221,9 @@ IPipWheelService pipWheelService
InitialValue = HardwareHelper.IterGpuInfo().Select(gpu => gpu.MemoryLevel).Max() switch
{
MemoryLevel.Low => "--lowvram",
MemoryLevel.Medium => "--normalvram",
Comment thread
NeuralFault marked this conversation as resolved.
_ => null,
},
Options = ["--highvram", "--normalvram", "--lowvram", "--novram"],
Options = ["--highvram", "--lowvram", "--novram"],
Comment thread
NeuralFault marked this conversation as resolved.
},
new()
{
Expand Down
61 changes: 61 additions & 0 deletions StabilityMatrix.Core/Services/SettingsManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -460,6 +460,8 @@ public void SetEulaAccepted()
/// </summary>
protected virtual void LoadSettings(CancellationToken cancellationToken = default)
{
var shouldPersistNormalizedSettings = false;

fileLock.Wait(cancellationToken);

try
Expand All @@ -478,13 +480,26 @@ protected virtual void LoadSettings(CancellationToken cancellationToken = defaul
}

Settings = DeserializeOrRecoverSettings(rawBytes);
shouldPersistNormalizedSettings = NormalizeLoadedSettings(Settings);
}
finally
{
fileLock.Release();

isLoaded = true;

if (shouldPersistNormalizedSettings)
{
try
{
SaveSettings(cancellationToken);
}
catch (Exception ex)
{
logger.LogWarning(ex, "Failed to persist normalized settings after load");
}
}

Loaded?.Invoke(this, EventArgs.Empty);
}
}
Expand All @@ -495,6 +510,8 @@ protected virtual void LoadSettings(CancellationToken cancellationToken = defaul
/// </summary>
protected virtual async Task LoadSettingsAsync(CancellationToken cancellationToken = default)
{
var shouldPersistNormalizedSettings = false;

await fileLock.WaitAsync(cancellationToken).ConfigureAwait(false);

try
Expand All @@ -514,17 +531,61 @@ protected virtual async Task LoadSettingsAsync(CancellationToken cancellationTok
}

Settings = DeserializeOrRecoverSettings(rawBytes);
shouldPersistNormalizedSettings = NormalizeLoadedSettings(Settings);
}
finally
{
fileLock.Release();

isLoaded = true;

if (shouldPersistNormalizedSettings)
{
try
{
await SaveSettingsAsync(cancellationToken).ConfigureAwait(false);
}
catch (Exception ex)
{
logger.LogWarning(ex, "Failed to persist normalized settings after load");
}
}

Loaded?.Invoke(this, EventArgs.Empty);
}
}

private bool NormalizeLoadedSettings(Settings settings)
{
var removedCount = 0;

foreach (var package in settings.InstalledPackages)
{
if (
package.PackageName?.StartsWith("ComfyUI", StringComparison.OrdinalIgnoreCase) != true
|| package.LaunchArgs is not { Count: > 0 }
)
{
continue;
}

removedCount += package.LaunchArgs.RemoveAll(option =>
option.Name.Equals("--normalvram", StringComparison.OrdinalIgnoreCase)
);
}

if (removedCount > 0)
{
logger.LogInformation(
"Removed {RemovedCount} obsolete ComfyUI launch args from loaded settings",
removedCount
);
return true;
}

return false;
}

/// <summary>
/// Attempts to deserialize settings from raw bytes, falling back to sanitization
/// and recovery if the JSON is corrupted. Returns default settings as a last resort.
Expand Down
117 changes: 117 additions & 0 deletions StabilityMatrix.Tests/Core/SettingsManagerTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
using System.Text.Json;
using Microsoft.Extensions.Logging.Abstractions;
using StabilityMatrix.Core.Models.Settings;
using StabilityMatrix.Core.Services;

namespace StabilityMatrix.Tests.Core;

[TestClass]
public class SettingsManagerTests
{
private string? tempDirectory;

[TestCleanup]
public void Cleanup()
{
if (tempDirectory is not null && Directory.Exists(tempDirectory))
{
Directory.Delete(tempDirectory, recursive: true);
}
}

[TestMethod]
public void TryFindLibrary_StripsObsoleteComfyNormalVramLaunchArgsAndPersists()
{
tempDirectory = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName());
Directory.CreateDirectory(tempDirectory);

var settingsPath = Path.Combine(tempDirectory, "settings.json");
File.WriteAllText(
settingsPath,
"""
{
"Version": 1,
"InstalledPackages": [
{
"Id": "11111111-1111-1111-1111-111111111111",
"PackageName": "ComfyUI",
"LaunchArgs": [
{
"Name": "--normalvram",
"Type": "Bool",
"OptionValue": true
},
{
"Name": "--lowvram",
"Type": "Bool",
"OptionValue": false
}
]
},
{
"Id": "22222222-2222-2222-2222-222222222222",
"PackageName": "ComfyUI-Zluda",
"LaunchArgs": [
{
"Name": "--normalvram",
"Type": "Bool",
"OptionValue": true
}
]
},
{
"Id": "33333333-3333-3333-3333-333333333333",
"PackageName": "OtherPackage",
"LaunchArgs": [
{
"Name": "--normalvram",
"Type": "Bool",
"OptionValue": true
}
]
}
]
}
"""
);

var settingsManager = new SettingsManager(NullLogger<SettingsManager>.Instance);
settingsManager.SetLibraryDirOverride(tempDirectory);

var wasFound = settingsManager.TryFindLibrary();

Assert.IsTrue(wasFound);

var comfyPackage = settingsManager.Settings.InstalledPackages.Single(package =>
package.PackageName == "ComfyUI"
);
var zludaPackage = settingsManager.Settings.InstalledPackages.Single(package =>
package.PackageName == "ComfyUI-Zluda"
);
var otherPackage = settingsManager.Settings.InstalledPackages.Single(package =>
package.PackageName == "OtherPackage"
);

Assert.IsFalse(comfyPackage.LaunchArgs!.Any(option => option.Name == "--normalvram"));
Assert.IsFalse(zludaPackage.LaunchArgs!.Any(option => option.Name == "--normalvram"));
Assert.IsTrue(otherPackage.LaunchArgs!.Any(option => option.Name == "--normalvram"));

var persistedSettings = JsonSerializer.Deserialize<Settings>(File.ReadAllText(settingsPath));

Assert.IsNotNull(persistedSettings);

var persistedComfyPackage = persistedSettings.InstalledPackages.Single(package =>
package.PackageName == "ComfyUI"
);
var persistedZludaPackage = persistedSettings.InstalledPackages.Single(package =>
package.PackageName == "ComfyUI-Zluda"
);
var persistedOtherPackage = persistedSettings.InstalledPackages.Single(package =>
package.PackageName == "OtherPackage"
);

Assert.IsFalse(persistedComfyPackage.LaunchArgs!.Any(option => option.Name == "--normalvram"));
Assert.IsFalse(persistedZludaPackage.LaunchArgs!.Any(option => option.Name == "--normalvram"));
Assert.IsTrue(persistedOtherPackage.LaunchArgs!.Any(option => option.Name == "--normalvram"));
}
}
Loading