Skip to content

Commit a8dbfc0

Browse files
committed
Moved --normalvram migration into ComfyUI/ComfyUI-Zluda launch flow.
- Removed obsolete SettingsManager migration test. Refactored it into standalone package-level test.
1 parent 62ab4c2 commit a8dbfc0

5 files changed

Lines changed: 120 additions & 180 deletions

File tree

StabilityMatrix.Core/Models/Packages/ComfyUI.cs

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -528,6 +528,8 @@ public override async Task RunPackage(
528528
await SetupVenv(installLocation, pythonVersion: PyVersion.Parse(installedPackage.PythonVersion))
529529
.ConfigureAwait(false);
530530

531+
var launchArguments = NormalizeLaunchArguments(installedPackage, options.Arguments);
532+
531533
VenvRunner.UpdateEnvironmentVariables(GetEnvVars);
532534

533535
// Check for old NVIDIA driver version with cu130 installations
@@ -584,7 +586,7 @@ older torch index (e.g. cu128)
584586
}
585587

586588
VenvRunner.RunDetached(
587-
[Path.Combine(installLocation, options.Command ?? LaunchCommand), .. options.Arguments],
589+
[Path.Combine(installLocation, options.Command ?? LaunchCommand), .. launchArguments],
588590
HandleConsoleOutput,
589591
OnExit
590592
);
@@ -608,6 +610,34 @@ void HandleConsoleOutput(ProcessOutput s)
608610
}
609611
}
610612

613+
protected ProcessArgs NormalizeLaunchArguments(
614+
InstalledPackage installedPackage,
615+
ProcessArgs fallbackArguments
616+
)
617+
{
618+
if (installedPackage.LaunchArgs is not { Count: > 0 })
619+
{
620+
return fallbackArguments;
621+
}
622+
623+
var removedCount = installedPackage.LaunchArgs.RemoveAll(option =>
624+
string.Equals(option.Name, "--normalvram", StringComparison.OrdinalIgnoreCase)
625+
);
626+
627+
if (removedCount == 0)
628+
{
629+
return fallbackArguments;
630+
}
631+
632+
Logger.Info("Removed {RemovedCount} obsolete ComfyUI launch args before launch", removedCount);
633+
634+
SettingsManager.SaveLaunchArgs(installedPackage.Id, installedPackage.LaunchArgs);
635+
636+
return ProcessArgs.FromQuoted(
637+
installedPackage.LaunchArgs.Select(option => option.ToArgString()).OfType<string>()
638+
);
639+
}
640+
611641
public override TorchIndex GetRecommendedTorchVersion()
612642
{
613643
var preferRocm =

StabilityMatrix.Core/Models/Packages/ComfyZluda.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -205,8 +205,10 @@ public override async Task RunPackage(
205205
await SetupVenv(installLocation, pythonVersion: PyVersion.Parse(installedPackage.PythonVersion))
206206
.ConfigureAwait(false);
207207

208+
var launchArguments = NormalizeLaunchArguments(installedPackage, options.Arguments);
209+
208210
var zludaPath = Path.Combine(installLocation, LaunchCommand);
209-
ProcessArgs args = ["--", VenvRunner.PythonPath.ToString(), "main.py", .. options.Arguments];
211+
ProcessArgs args = ["--", VenvRunner.PythonPath.ToString(), "main.py", .. launchArguments];
210212
zludaProcess = ProcessRunner.StartAnsiProcess(
211213
zludaPath,
212214
args,

StabilityMatrix.Core/Services/SettingsManager.cs

Lines changed: 0 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -460,8 +460,6 @@ public void SetEulaAccepted()
460460
/// </summary>
461461
protected virtual void LoadSettings(CancellationToken cancellationToken = default)
462462
{
463-
var shouldPersistNormalizedSettings = false;
464-
465463
fileLock.Wait(cancellationToken);
466464

467465
try
@@ -480,26 +478,13 @@ protected virtual void LoadSettings(CancellationToken cancellationToken = defaul
480478
}
481479

482480
Settings = DeserializeOrRecoverSettings(rawBytes);
483-
shouldPersistNormalizedSettings = NormalizeLoadedSettings(Settings);
484481
}
485482
finally
486483
{
487484
fileLock.Release();
488485

489486
isLoaded = true;
490487

491-
if (shouldPersistNormalizedSettings)
492-
{
493-
try
494-
{
495-
SaveSettings(cancellationToken);
496-
}
497-
catch (Exception ex)
498-
{
499-
logger.LogWarning(ex, "Failed to persist normalized settings after load");
500-
}
501-
}
502-
503488
Loaded?.Invoke(this, EventArgs.Empty);
504489
}
505490
}
@@ -510,8 +495,6 @@ protected virtual void LoadSettings(CancellationToken cancellationToken = defaul
510495
/// </summary>
511496
protected virtual async Task LoadSettingsAsync(CancellationToken cancellationToken = default)
512497
{
513-
var shouldPersistNormalizedSettings = false;
514-
515498
await fileLock.WaitAsync(cancellationToken).ConfigureAwait(false);
516499

517500
try
@@ -531,61 +514,17 @@ protected virtual async Task LoadSettingsAsync(CancellationToken cancellationTok
531514
}
532515

533516
Settings = DeserializeOrRecoverSettings(rawBytes);
534-
shouldPersistNormalizedSettings = NormalizeLoadedSettings(Settings);
535517
}
536518
finally
537519
{
538520
fileLock.Release();
539521

540522
isLoaded = true;
541523

542-
if (shouldPersistNormalizedSettings)
543-
{
544-
try
545-
{
546-
await SaveSettingsAsync(cancellationToken).ConfigureAwait(false);
547-
}
548-
catch (Exception ex)
549-
{
550-
logger.LogWarning(ex, "Failed to persist normalized settings after load");
551-
}
552-
}
553-
554524
Loaded?.Invoke(this, EventArgs.Empty);
555525
}
556526
}
557527

558-
private bool NormalizeLoadedSettings(Settings settings)
559-
{
560-
var removedCount = 0;
561-
562-
foreach (var package in settings.InstalledPackages)
563-
{
564-
if (
565-
package.PackageName?.StartsWith("ComfyUI", StringComparison.OrdinalIgnoreCase) != true
566-
|| package.LaunchArgs is not { Count: > 0 }
567-
)
568-
{
569-
continue;
570-
}
571-
572-
removedCount += package.LaunchArgs.RemoveAll(option =>
573-
option.Name.Equals("--normalvram", StringComparison.OrdinalIgnoreCase)
574-
);
575-
}
576-
577-
if (removedCount > 0)
578-
{
579-
logger.LogInformation(
580-
"Removed {RemovedCount} obsolete ComfyUI launch args from loaded settings",
581-
removedCount
582-
);
583-
return true;
584-
}
585-
586-
return false;
587-
}
588-
589528
/// <summary>
590529
/// Attempts to deserialize settings from raw bytes, falling back to sanitization
591530
/// and recovery if the JSON is corrupted. Returns default settings as a last resort.

StabilityMatrix.Tests/Core/SettingsManagerTests.cs

Lines changed: 0 additions & 117 deletions
This file was deleted.
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
using NSubstitute;
2+
using StabilityMatrix.Core.Helper;
3+
using StabilityMatrix.Core.Helper.Cache;
4+
using StabilityMatrix.Core.Models;
5+
using StabilityMatrix.Core.Models.Packages;
6+
using StabilityMatrix.Core.Processes;
7+
using StabilityMatrix.Core.Python;
8+
using StabilityMatrix.Core.Services;
9+
10+
namespace StabilityMatrix.Tests.Models.Packages;
11+
12+
[TestClass]
13+
public class ComfyLaunchArgMigrationTests
14+
{
15+
[TestMethod]
16+
public void NormalizeLaunchArguments_StripsObsoleteNormalVramAndPersistsUpdatedArgs()
17+
{
18+
var settingsManager = Substitute.For<ISettingsManager>();
19+
var package = new TestComfyUI(
20+
Substitute.For<IGithubApiCache>(),
21+
settingsManager,
22+
Substitute.For<IDownloadService>(),
23+
Substitute.For<IPrerequisiteHelper>(),
24+
Substitute.For<IPyInstallationManager>(),
25+
Substitute.For<IPipWheelService>()
26+
);
27+
28+
var installedPackage = new InstalledPackage
29+
{
30+
Id = Guid.NewGuid(),
31+
PackageName = "ComfyUI",
32+
LaunchArgs =
33+
[
34+
new LaunchOption
35+
{
36+
Name = "--normalvram",
37+
Type = LaunchOptionType.Bool,
38+
OptionValue = true,
39+
},
40+
],
41+
};
42+
43+
var fallbackArguments = ProcessArgs.FromQuoted(
44+
installedPackage
45+
.LaunchArgs.Select(option => option.ToArgString())
46+
.Where(argument => argument is not null)
47+
.Select(argument => argument!)
48+
);
49+
50+
var normalizedArguments = package.Normalize(installedPackage, fallbackArguments);
51+
52+
Assert.IsFalse(installedPackage.LaunchArgs.Any(option => option.Name == "--normalvram"));
53+
Assert.IsFalse(normalizedArguments.Contains("--normalvram"));
54+
settingsManager
55+
.Received(1)
56+
.SaveLaunchArgs(
57+
installedPackage.Id,
58+
Arg.Is<IEnumerable<LaunchOption>>(options =>
59+
options.All(option => option.Name != "--normalvram")
60+
)
61+
);
62+
}
63+
64+
private sealed class TestComfyUI : ComfyUI
65+
{
66+
public TestComfyUI(
67+
IGithubApiCache githubApi,
68+
ISettingsManager settingsManager,
69+
IDownloadService downloadService,
70+
IPrerequisiteHelper prerequisiteHelper,
71+
IPyInstallationManager pyInstallationManager,
72+
IPipWheelService pipWheelService
73+
)
74+
: base(
75+
githubApi,
76+
settingsManager,
77+
downloadService,
78+
prerequisiteHelper,
79+
pyInstallationManager,
80+
pipWheelService
81+
) { }
82+
83+
public ProcessArgs Normalize(InstalledPackage installedPackage, ProcessArgs fallbackArguments) =>
84+
NormalizeLaunchArguments(installedPackage, fallbackArguments);
85+
}
86+
}

0 commit comments

Comments
 (0)