diff --git a/docs/SETUP.md b/docs/SETUP.md index b8cc649822..47058c5392 100644 --- a/docs/SETUP.md +++ b/docs/SETUP.md @@ -194,6 +194,10 @@ OpenClaw Companion checks for updates automatically and shows a notification whe Go to **Settings → Apps → Installed apps**, find **OpenClaw Companion**, and click **Uninstall**. Alternatively, use **Add or Remove Programs** in the Control Panel. You'll be asked whether to also remove the local WSL gateway; choose **Yes** to unregister its WSL distro and generated state, or **No** to leave the gateway and that state in place. +For the **Inno installer**, interactive uninstall offers an unchecked Kokoro download-removal choice, independent of Gateway removal. Continue immediately to keep models, or choose **Check downloaded models** to see exact file paths and measured byte counts before enabling removal. The read-only check runs asynchronously, leaving Cancel available. Cancellation waits for the worker result before closing; an unconfirmed result aborts uninstall rather than enabling removal. Keeping models is the default, including silent uninstall. Only the catalog model and voices files whose current contents match their pinned hashes are eligible; other models, partial downloads, unknown files, shared caches, and directories are kept. Redirected paths, hard links, locked files, and unverified contents are not removed. Store migration or uncertain ownership suppresses removal at its final admission check. A custom `OPENCLAW_TRAY_DATA_DIR` disables this cleanup option. + +The model summary distinguishes removed bytes, measured retained catalog files, and files that could not be checked. Other app-data sizes are not scanned, so these numbers are not a total of all retained data. Cancelling the model choice stops uninstall before model removal; cleanup failures leave an incomplete summary rather than a claim that everything was removed. This option does not change MSIX uninstall or the separate Gateway cleanup scope. + Your settings file at `%APPDATA%\OpenClawTray\settings.json` is not removed automatically, and device identity files for gateways unrelated to the one you removed (including per-gateway keys at `%APPDATA%\OpenClawTray\gateways\\device-key-ed25519.json`) are preserved. Choosing **Yes** also removes the removed gateway's own identity directory. Root operator and node tokens are preserved only while an external gateway record remains; otherwise they are cleared, even if another local or loopback gateway record remains. Delete `%APPDATA%\OpenClawTray\` manually if you want a fully clean uninstall. For the **Companion MSIX**, managed Gateway cleanup uses a current-user interactive scheduled task that survives Companion removal. Its worker, registration script and versioned `cleanup-host-.exe` are copied to `%USERPROFILE%\.openclaw-companion\cleanup\\`. The task runs at logon and every minute through a GUI-subsystem helper that starts Windows PowerShell with `CreateNoWindow`, preventing a desktop console from being created. The helper targets the Windows-provided .NET Framework 4.8 and forwards the worker's exit code, recording failures in `cleanup-host-error.txt` beside the worker. It does not depend on VBScript, the Companion's .NET 10 runtime, or an executable inside the removed package. Versioned helper paths allow an updated Companion to refresh the task without overwriting a running helper. diff --git a/installer.iss b/installer.iss index da9e9b6d25..43be39d17f 100644 --- a/installer.iss +++ b/installer.iss @@ -114,6 +114,11 @@ Source: "{#publish}\*"; DestDir: "{app}"; Flags: ignoreversion recursesubdirs Source: "scripts\Uninstall-LocalGateway.ps1"; DestDir: "{app}"; Flags: ignoreversion Source: "scripts\Test-InnoMigration.ps1"; DestDir: "{app}"; Flags: ignoreversion Source: "src\OpenClaw.Connection\Migration\MigrationRecordCodec.cs"; DestDir: "{app}"; Flags: ignoreversion +; The model helper compiles the same pinned catalog and file operation as Shared. +; It does not load Companion or keep its installed binaries locked. +Source: "scripts\Uninstall-DownloadedModels.ps1"; DestDir: "{app}"; Flags: ignoreversion +Source: "src\OpenClaw.Shared\Audio\KokoroDownloadCatalog.cs"; DestDir: "{app}"; Flags: ignoreversion +Source: "src\OpenClaw.Shared\Audio\KokoroUninstallFiles.cs"; DestDir: "{app}"; Flags: ignoreversion #if vcRedist != "" Source: "{#vcRedist}"; DestDir: "{tmp}"; DestName: "vc_redist.exe"; Flags: deleteafterinstall; AfterInstall: InstallVCRuntime #endif @@ -148,6 +153,10 @@ Name: "{userstartup}\{#MyAppName}"; Filename: "{app}\{#MyAppExeName}"; Parameter Filename: "{app}\{#MyAppExeName}"; Description: "{cm:LaunchProgram,{#StringChange(MyAppName, '&', '&&')}}"; Flags: nowait postinstall skipifsilent; Check: ShouldLaunchTray [Code] +const + ModelPreviewPollMilliseconds = 100; + ModelStartupGraceSeconds = 30; + ModelStopGraceSeconds = 10; var VCRuntimeInstallSucceeded: Boolean; LocalGatewayCleanupChoiceInitialized: Boolean; @@ -156,6 +165,27 @@ var MigrationOperationHandle: THandle; MigrationOperationLocked: Boolean; MigrationOperationUnavailable: Boolean; + ModelCleanupSelection: Integer; + ModelSummary: String; + ModelScanRunning, ModelScanCancelled, ModelScanAllowed: Boolean; + ModelPreviewEligible: Integer; + ModelDialog: TSetupForm; + ModelDetails: TNewMemo; + ModelRemoveCheck: TNewCheckBox; + ModelScanButton, ModelContinueButton: TNewButton; + ModelPreviewTimer: THandle; + ModelPreviewDeadline: Int64; + ModelInvocation: Integer; + ModelReportPath, ModelCancelPath: String; + +function SetModelTimer(Window: THandle; Id: THandle; Interval: LongWord; Callback: THandle): THandle; + external 'SetTimer@user32.dll stdcall'; +function KillModelTimer(Window: THandle; Id: THandle): Boolean; + external 'KillTimer@user32.dll stdcall'; +function ModelTickCount: Int64; + external 'GetTickCount64@kernel32.dll stdcall'; + +function ChooseModelCleanup: Boolean; forward; function OpenMigrationOperationFile( FileName: String; DesiredAccess, ShareMode: LongWord; SecurityAttributes: Integer; @@ -273,6 +303,8 @@ begin if Result and MigrationOperationUnavailable then Log('Migration state could not be locked. Uninstall continues and preserves the local gateway.'); #endif + if Result then + Result := ChooseModelCleanup; end; procedure DeinitializeUninstall; @@ -521,6 +553,265 @@ begin MigrateLegacyBackgroundLaunch; end; +function ReadModelCompletion(var Code: Integer): Boolean; +var + Lines: TArrayOfString; + I: Integer; +begin + Result := False; + Code := -1; + if not LoadStringsFromFile(ModelReportPath, Lines) then Exit; + if GetArrayLength(Lines) < 2 then Exit; + Code := StrToIntDef(Lines[0], -1); + if (Code < 32) or (Code > 50) then Exit; + ModelSummary := ''; + for I := 1 to GetArrayLength(Lines) - 1 do + begin + if I > 1 then ModelSummary := ModelSummary + #13#10; + ModelSummary := ModelSummary + Lines[I]; + end; + Result := True; +end; + +function StageModelHelper: Boolean; +begin + Result := CopyFile(ExpandConstant('{app}\Uninstall-DownloadedModels.ps1'), + ExpandConstant('{tmp}\Uninstall-DownloadedModels.ps1'), False); + if not Result then Exit; + Result := CopyFile(ExpandConstant('{app}\KokoroDownloadCatalog.cs'), + ExpandConstant('{tmp}\KokoroDownloadCatalog.cs'), False); + if not Result then Exit; + Result := CopyFile(ExpandConstant('{app}\KokoroUninstallFiles.cs'), + ExpandConstant('{tmp}\KokoroUninstallFiles.cs'), False); +end; + +function StartModelHelper(Selection: Integer; Wait: TExecWait; var Code: Integer): Boolean; +var + Params: String; +begin + Inc(ModelInvocation); + ModelReportPath := ExpandConstant('{tmp}\model-result-') + IntToStr(ModelInvocation) + '.txt'; + ModelCancelPath := ExpandConstant('{tmp}\model-cancel-') + IntToStr(ModelInvocation) + '.txt'; + Params := '-NoProfile -NonInteractive -ExecutionPolicy Bypass -File ' + + AddQuotes(ExpandConstant('{tmp}\Uninstall-DownloadedModels.ps1')) + + ' -DataDirectory ' + AddQuotes(ExpandConstant('{userappdata}\{#MyInstallDir}')) + + ' -ReportPath ' + AddQuotes(ModelReportPath) + + ' -CancellationPath ' + AddQuotes(ModelCancelPath) + + ' -SelectionMask ' + IntToStr(Selection); + if Selection <> 0 then Params := Params + ' -ConfirmDestructive'; + Result := Exec(ExpandConstant('{sys}\WindowsPowerShell\v1.0\powershell.exe'), + Params, '', SW_HIDE, Wait, Code); +end; + +procedure StopModelPreviewTimer; +begin + if ModelPreviewTimer <> 0 then + begin + KillModelTimer(0, ModelPreviewTimer); + ModelPreviewTimer := 0; + end; +end; + +procedure FinishModelPreview; +begin + StopModelPreviewTimer; + ModelScanRunning := False; + ModelDetails.Text := ModelSummary; + ModelRemoveCheck.Enabled := ModelPreviewEligible <> 0; + ModelScanButton.Enabled := ModelScanAllowed; + ModelContinueButton.Enabled := True; + if ModelScanCancelled then ModelDialog.Close; +end; + +procedure CancelModelPreview(Sender: TObject); +begin + if ModelScanRunning then + begin + ModelScanCancelled := True; + SaveStringToFile(ModelCancelPath, 'cancel', False); + ModelDetails.Text := 'Cancelling the model check. Waiting for the worker to stop; no model removal has started.'; + end + else + ModelDialog.Close; +end; + +procedure ModelPreviewCloseQuery(Sender: TObject; var CanClose: Boolean); +begin + CanClose := not ModelScanRunning; + if not CanClose then CancelModelPreview(Sender); +end; + +procedure ModelPreviewTick(Window: THandle; Message: LongWord; Timer: THandle; Time: LongWord); +var + Code: Integer; +begin + if (Timer <> ModelPreviewTimer) or not ModelScanRunning then Exit; + if FileExists(ModelReportPath) then + begin + // This file is atomically published by the supervisor, never by the worker. + // A late receipt from another invocation has a different filename. + if not ReadModelCompletion(Code) then Code := 50; + if Code = 50 then + begin + StopModelPreviewTimer; + SaveStringToFile(ModelCancelPath, 'cancel', False); + ModelScanCancelled := True; + MsgBox('The model check could not confirm completion. Uninstall will stop without starting model removal.', mbError, MB_OK); + end + else if (Code >= 32) and (Code <= 47) then + ModelPreviewEligible := Code - 32; + FinishModelPreview; + end + else if ModelTickCount >= ModelPreviewDeadline then + begin + StopModelPreviewTimer; + SaveStringToFile(ModelCancelPath, 'cancel', False); + ModelScanCancelled := True; + MsgBox('The model check did not report completion within its time limit. Uninstall will stop; worker exit is unconfirmed.', mbError, MB_OK); + FinishModelPreview; + end; +end; + +procedure CheckModelPreview(Sender: TObject); +var + Code, Seconds: Integer; +begin + ModelPreviewEligible := 0; + ModelRemoveCheck.Checked := False; + ModelRemoveCheck.Enabled := False; + if not StageModelHelper then + begin + ModelDetails.Text := 'The model helper is unavailable. Models are kept.'; + Exit; + end; + ModelScanRunning := True; + ModelScanCancelled := False; + ModelScanButton.Enabled := False; + ModelContinueButton.Enabled := False; + ModelDetails.Text := 'Checking downloaded model files. Cancel stops this read-only check.'; + Seconds := StrToIntDef(GetEnv('OPENCLAW_MODEL_UNINSTALL_PROCESS_TIMEOUT_SECONDS'), 180); + if (Seconds < 30) or (Seconds > 900) then Seconds := 180; + ModelPreviewDeadline := ModelTickCount + (Seconds + ModelStartupGraceSeconds + ModelStopGraceSeconds) * 1000; + ModelPreviewTimer := SetModelTimer(0, 0, ModelPreviewPollMilliseconds, CreateCallback(@ModelPreviewTick)); + if ModelPreviewTimer = 0 then + begin + ModelSummary := 'The model check could not start. Models are kept.'; + FinishModelPreview; + Exit; + end; + // No synchronous wait: Inno must not disable the custom form while scanning. + // Preview is read-only. Store ownership is rechecked at the deletion boundary. + if not StartModelHelper(0, ewNoWait, Code) then + begin + ModelSummary := 'The model helper could not start. Models are kept.'; + FinishModelPreview; + end; +end; + +function ShowModelReport(Choosing: Boolean): Boolean; +var + CancelButton: TNewButton; +begin + ModelDialog := CreateCustomForm(ScaleX(640), ScaleY(440), True, True); + try + ModelDialog.Caption := 'Downloaded voice models'; + ModelDialog.OnCloseQuery := @ModelPreviewCloseQuery; + ModelRemoveCheck := TNewCheckBox.Create(ModelDialog); + ModelRemoveCheck.Parent := ModelDialog; + ModelRemoveCheck.SetBounds(ScaleX(16), ScaleY(16), ScaleX(608), ScaleY(32)); + ModelRemoveCheck.Caption := 'Remove the verified Kokoro downloads listed below'; + ModelRemoveCheck.Checked := False; + ModelRemoveCheck.Enabled := False; + ModelRemoveCheck.Visible := Choosing; + ModelDetails := TNewMemo.Create(ModelDialog); + ModelDetails.Parent := ModelDialog; + ModelDetails.SetBounds(ScaleX(16), ScaleY(56), ScaleX(608), ScaleY(320)); + ModelDetails.ReadOnly := True; + ModelDetails.ScrollBars := ssVertical; + ModelDetails.WordWrap := True; + ModelDetails.Text := ModelSummary; + ModelScanButton := TNewButton.Create(ModelDialog); + ModelScanButton.Parent := ModelDialog; + ModelScanButton.SetBounds(ScaleX(16), ScaleY(392), ScaleX(240), ScaleY(32)); + ModelScanButton.Caption := 'Check downloaded models'; + ModelScanButton.Enabled := ModelScanAllowed; + ModelScanButton.Visible := Choosing; + ModelScanButton.OnClick := @CheckModelPreview; + ModelContinueButton := TNewButton.Create(ModelDialog); + ModelContinueButton.Parent := ModelDialog; + ModelContinueButton.SetBounds(ScaleX(352), ScaleY(392), ScaleX(152), ScaleY(32)); + ModelContinueButton.ModalResult := mrOk; + ModelContinueButton.Default := True; + if Choosing then ModelContinueButton.Caption := 'Continue uninstall' + else ModelContinueButton.Caption := 'Close'; + CancelButton := TNewButton.Create(ModelDialog); + CancelButton.Parent := ModelDialog; + CancelButton.SetBounds(ScaleX(512), ScaleY(392), ScaleX(112), ScaleY(32)); + CancelButton.Caption := 'Cancel'; + CancelButton.OnClick := @CancelModelPreview; + CancelButton.Cancel := True; + CancelButton.Visible := Choosing; + Result := ModelDialog.ShowModal = mrOk; + if Choosing and Result and ModelRemoveCheck.Checked then + ModelCleanupSelection := ModelPreviewEligible; + finally + StopModelPreviewTimer; + ModelDialog.Free; + end; +end; + +function ChooseModelCleanup: Boolean; +begin + Result := True; + ModelCleanupSelection := 0; + ModelPreviewEligible := 0; + ModelScanRunning := False; + ModelScanAllowed := False; + ModelSummary := 'Downloaded models and other app data will be kept at ' + + ExpandConstant('{userappdata}\{#MyInstallDir}') + '.' + #13#10 + + 'Sizes have not been measured. Gateway removal is a separate choice.'; + if UninstallSilent() then Exit; + if GetEnv('OPENCLAW_TRAY_DATA_DIR') <> '' then + ModelSummary := 'A custom app-data directory is configured. Models are kept; sizes were not scanned.' + #13#10 + + 'Configured path: ' + GetEnv('OPENCLAW_TRAY_DATA_DIR') + #13#10 + ModelSummary + else if MigrationOperationUnavailable then + ModelSummary := 'Migration ownership could not be checked. Models are kept.' + #13#10 + ModelSummary + else + begin + ModelScanAllowed := True; + ModelSummary := ModelSummary + #13#10#13#10 + + 'Continue to keep models, or check downloaded models to preview files eligible for optional removal.'; + end; + // Keeping models needs no scan. The visible dialog owns the optional check. + Result := ShowModelReport(True); +end; + +procedure RunSelectedModelCleanup; +var + Code, ReportCode: Integer; +begin + if ModelCleanupSelection = 0 then Exit; + // Preserve on changed Store ownership, independently of the Gateway choice. + if MigrationOperationUnavailable then + begin + ModelSummary := 'Models were kept: migration ownership is unavailable.' + #13#10 + ModelSummary; + Exit; + end; + if CheckCompletedStoreMigration <> 0 then + begin + ModelSummary := 'Models were kept: Store migration or ownership changed.' + #13#10 + ModelSummary; + Exit; + end; + UninstallProgressForm.StatusLabel.Caption := 'Checking and removing selected voice models...'; + if StartModelHelper(ModelCleanupSelection, ewWaitUntilTerminated, Code) then + begin + if ReadModelCompletion(ReportCode) then + if (Code = 32) and (ReportCode = Code) then Exit; + end; + ModelSummary := 'Model cleanup could not be completed or verified. Some selected files may remain.' + #13#10 + + ModelSummary; +end; + procedure EnsureLocalGatewayCleanupChoice; var MigrationResult: Integer; @@ -736,6 +1027,7 @@ procedure CurUninstallStepChanged(CurUninstallStep: TUninstallStep); begin if CurUninstallStep = usUninstall then begin + RunSelectedModelCleanup; RemoveAppAutoStart; EnsureLocalGatewayCleanupChoice; RunLocalGatewayCleanup; @@ -743,5 +1035,11 @@ begin else if CurUninstallStep = usPostUninstall then begin DeleteGeneratedAppState; + if not UninstallSilent() then + begin + if ModelCleanupSelection = 0 then + ModelSummary := 'No model removal was requested. Measurements below are from the preview.' + #13#10 + ModelSummary; + ShowModelReport(False); + end; end; end; diff --git a/scripts/Uninstall-DownloadedModels.ps1 b/scripts/Uninstall-DownloadedModels.ps1 new file mode 100644 index 0000000000..bf222e8649 --- /dev/null +++ b/scripts/Uninstall-DownloadedModels.ps1 @@ -0,0 +1,101 @@ +<# +.SYNOPSIS + Preview or explicitly remove only verified Kokoro downloads for Inno uninstall. +.DESCRIPTION + The installer owns consent and the migration lease. The worker re-verifies + selected files and deletes through their pinned handles. No recursive cleanup. + The supervisor atomically publishes one completion report AFTER worker exit: + first line is 32..47 (preview mask = code - 32), 32 (removal complete), + 48 (partial removal), 49 (stopped, failed/cancelled), or 50 (exit unconfirmed). + Remaining lines are the human-readable report. Launch alone is not completion. +#> +[CmdletBinding()] +param( + [Parameter(Mandatory = $true)][string]$DataDirectory, + [Parameter(Mandatory = $true)][string]$ReportPath, + [ValidateRange(0, 15)][int]$SelectionMask = 0, + [string]$CancellationPath = '', + [switch]$ConfirmDestructive, + [switch]$Worker +) + +$ErrorActionPreference = 'Stop' +$encoding = New-Object System.Text.UTF8Encoding $true +$failureText = "Model operation could not be completed. Sizes or removal may be incomplete.$([Environment]::NewLine)Model path: $DataDirectory\models\kokoro" + +if ($Worker) { + try { + if ($SelectionMask -ne 0 -and -not $ConfirmDestructive) { throw 'Explicit removal confirmation is missing.' } + Add-Type -Path @((Join-Path $PSScriptRoot 'KokoroDownloadCatalog.cs'), + (Join-Path $PSScriptRoot 'KokoroUninstallFiles.cs')) -ErrorAction Stop + $result = [OpenClaw.Shared.Audio.KokoroUninstallFiles]::Run($DataDirectory, $SelectionMask) + [IO.File]::WriteAllText($ReportPath, $result.Text, $encoding) + if ($SelectionMask -eq 0) { exit (32 + $result.EligibleMask) } + if (-not $result.Complete) { exit 48 } + exit 32 + } catch { + [IO.File]::WriteAllText($ReportPath, $failureText + [Environment]::NewLine + $_.Exception.Message, $encoding) + exit 49 + } +} + +$child = $null +$stopped = $true +$code = 49 +$report = $failureText +try { + if ($SelectionMask -ne 0 -and -not $ConfirmDestructive) { throw 'Explicit removal confirmation is missing.' } + $seconds = 180 + $configured = 0 + if ([int]::TryParse($env:OPENCLAW_MODEL_UNINSTALL_PROCESS_TIMEOUT_SECONDS, [ref]$configured) -and + $configured -ge 30 -and $configured -le 900) { $seconds = $configured } + $cancellationPollMilliseconds = 100 + $terminationWaitMilliseconds = 10000 + if ($CancellationPath -and [IO.File]::Exists($CancellationPath)) { + throw 'Model check cancelled before starting. No model removal was started.' + } + $workerReport = $ReportPath + '.worker' + $arguments = @('-NoProfile', '-NonInteractive', '-ExecutionPolicy', 'Bypass', '-File', + $PSCommandPath, '-DataDirectory', $DataDirectory, '-ReportPath', $workerReport, + '-SelectionMask', [string]$SelectionMask, '-Worker') + if ($ConfirmDestructive) { $arguments += '-ConfirmDestructive' } + $quoted = foreach ($argument in $arguments) { + if ($argument.Contains('"')) { throw 'Unexpected quote in uninstall path.' } + '"' + ($argument -replace '(\\+)$', '$1$1') + '"' + } + $start = New-Object System.Diagnostics.ProcessStartInfo + $start.FileName = Join-Path $PSHOME 'powershell.exe' + $start.Arguments = $quoted -join ' ' + $start.UseShellExecute = $false + $start.CreateNoWindow = $true + $child = [Diagnostics.Process]::Start($start) + $stopped = $false + $clock = [Diagnostics.Stopwatch]::StartNew() + while (-not $child.WaitForExit($cancellationPollMilliseconds)) { + $cancelled = $CancellationPath -and [IO.File]::Exists($CancellationPath) + if ($cancelled -or $clock.Elapsed.TotalSeconds -ge $seconds) { + $child.Kill() + $stopped = $child.WaitForExit($terminationWaitMilliseconds) + if (-not $stopped) { throw 'Worker exit is unconfirmed. Uninstall must stop; do not retry cleanup.' } + if ($cancelled -and $SelectionMask -eq 0) { throw 'Model check cancelled. No model removal was started.' } + throw 'Model operation interrupted. Removal may be partial; inspect the model paths before retrying.' + } + } + $stopped = $true + $code = $child.ExitCode + if ($code -lt 32 -or $code -gt 49) { throw "Unexpected model worker exit code: $code" } + $report = [IO.File]::ReadAllText($workerReport) +} catch { + if (-not $stopped -and $null -ne $child) { + try { $stopped = $child.HasExited } catch { } + } + $code = if ($stopped) { 49 } else { 50 } + $report = $failureText + [Environment]::NewLine + $_.Exception.Message +} finally { + if ($null -ne $child) { $child.Dispose() } +} + +# A unique per-invocation path is supplied by Inno. A partial write is never a receipt. +[IO.File]::WriteAllText($ReportPath + '.pending', [string]$code + [Environment]::NewLine + $report, $encoding) +[IO.File]::Move($ReportPath + '.pending', $ReportPath) +exit $code diff --git a/src/OpenClaw.Shared/Audio/KokoroDownloadCatalog.cs b/src/OpenClaw.Shared/Audio/KokoroDownloadCatalog.cs new file mode 100644 index 0000000000..1878926a7d --- /dev/null +++ b/src/OpenClaw.Shared/Audio/KokoroDownloadCatalog.cs @@ -0,0 +1,38 @@ +// Pinned Kokoro downloads shared by the app and the Inno uninstall helper. +// Keep this file compatible with Windows PowerShell 5.1 Add-Type (.NET Framework). +// Example: KokoroDownloadCatalog.EnglishModel describes one verified download. +namespace OpenClaw.Shared.Audio +{ + internal sealed class KokoroDownloadAsset + { + internal readonly string PackId, FileName, Url, Sha256; + internal readonly long SizeBytes; + + internal KokoroDownloadAsset(string packId, string fileName, string url, string sha256, long sizeBytes) + { + PackId = packId; FileName = fileName; Url = url; Sha256 = sha256; SizeBytes = sizeBytes; + } + } + + internal static class KokoroDownloadCatalog + { + private const string Release = "https://github.com/larroy/kokoro.cpp/releases/download/voices_model_files/"; + internal static readonly KokoroDownloadAsset EnglishModel = new KokoroDownloadAsset( + "v1.1-en-zh", "kokoro-v1.1-zh.onnx", Release + "kokoro-v1.1-zh.onnx", + "eefec708cbc7aba8e8129b5c2f7cb92e1fe7d281af1e1dd451592d9ff0714a0d", 343605188); + internal static readonly KokoroDownloadAsset EnglishVoices = new KokoroDownloadAsset( + "v1.1-en-zh", "voices-v1.1-zh.bin", Release + "voices-v1.1-zh.bin", + "e678019845e6cfe3b7c34531779396b28f509451b91e6535d5dc09bbf11a4be5", 53792177); + internal static readonly KokoroDownloadAsset SpanishModel = new KokoroDownloadAsset( + "v1.0-es", "kokoro-v1.0.onnx", + "https://github.com/thewh1teagle/kokoro-onnx/releases/download/model-files-v1.0/kokoro-v1.0.onnx", + "7d5df8ecf7d4b1878015a32686053fd0eebe2bc377234608764cc0ef3636a6c5", 325532387); + internal static readonly KokoroDownloadAsset SpanishVoices = new KokoroDownloadAsset( + "v1.0-es", "voices-v1.0-es.bin", Release + "voices-v1.0-es.bin", + "cdaf0ecca101f3738763f6e3a13b63d46137909b8ebcf9de2be4ef2a73014c52", 1566778); + + // Stable bit positions identify exactly the files shown in the uninstall preview. + internal static readonly KokoroDownloadAsset[] All = + { EnglishModel, EnglishVoices, SpanishModel, SpanishVoices }; + } +} diff --git a/src/OpenClaw.Shared/Audio/KokoroModelManager.cs b/src/OpenClaw.Shared/Audio/KokoroModelManager.cs index 55a284fb60..cb2bfec29a 100644 --- a/src/OpenClaw.Shared/Audio/KokoroModelManager.cs +++ b/src/OpenClaw.Shared/Audio/KokoroModelManager.cs @@ -34,7 +34,6 @@ public sealed class KokoroModelManager public const string DefaultVoiceId = "af_maple"; private const string InstallMarkerSuffix = ".installing"; - private const string LarroyReleaseBase = "https://github.com/larroy/kokoro.cpp/releases/download/voices_model_files/"; // Static so two managers over the same data directory coalesce against the // same in-flight download. Ordinal: pack ids are fixed catalog keys. @@ -68,10 +67,8 @@ public sealed class KokoroModelManager public static readonly KokoroModelPackInfo[] AvailablePacks = [ new("v1.1-en-zh", "English and Chinese (Kokoro v1.1)", - new KokoroAssetInfo("kokoro-v1.1-zh.onnx", LarroyReleaseBase + "kokoro-v1.1-zh.onnx", - "eefec708cbc7aba8e8129b5c2f7cb92e1fe7d281af1e1dd451592d9ff0714a0d", 343_605_188), - new KokoroAssetInfo("voices-v1.1-zh.bin", LarroyReleaseBase + "voices-v1.1-zh.bin", - "e678019845e6cfe3b7c34531779396b28f509451b91e6535d5dc09bbf11a4be5", 53_792_177), + ModelAsset(KokoroDownloadCatalog.EnglishModel), + ModelAsset(KokoroDownloadCatalog.EnglishVoices), [ new("af_maple", "English (US): Maple", "en-US"), new("af_sol", "English (US): Sol", "en-US"), @@ -79,11 +76,8 @@ public sealed class KokoroModelManager .. ChineseVoiceIds.Select(id => new KokoroVoiceInfo(id, $"中文 (CN): {id}", "zh-CN")), ]), new("v1.0-es", "Español (Kokoro v1.0)", - new KokoroAssetInfo("kokoro-v1.0.onnx", - "https://github.com/thewh1teagle/kokoro-onnx/releases/download/model-files-v1.0/kokoro-v1.0.onnx", - "7d5df8ecf7d4b1878015a32686053fd0eebe2bc377234608764cc0ef3636a6c5", 325_532_387), - new KokoroAssetInfo("voices-v1.0-es.bin", LarroyReleaseBase + "voices-v1.0-es.bin", - "cdaf0ecca101f3738763f6e3a13b63d46137909b8ebcf9de2be4ef2a73014c52", 1_566_778), + ModelAsset(KokoroDownloadCatalog.SpanishModel), + ModelAsset(KokoroDownloadCatalog.SpanishVoices), [ new("ef_dora", "Español (ES): Dora", "es-ES"), new("em_alex", "Español (ES): Alex", "es-ES"), @@ -91,6 +85,9 @@ public sealed class KokoroModelManager ]), ]; + private static KokoroAssetInfo ModelAsset(KokoroDownloadAsset asset) => + new(asset.FileName, asset.Url, asset.Sha256, asset.SizeBytes); + private readonly string _packsDirectory; private readonly IOpenClawLogger _logger; diff --git a/src/OpenClaw.Shared/Audio/KokoroUninstallFiles.cs b/src/OpenClaw.Shared/Audio/KokoroUninstallFiles.cs new file mode 100644 index 0000000000..6f687ab9ac --- /dev/null +++ b/src/OpenClaw.Shared/Audio/KokoroUninstallFiles.cs @@ -0,0 +1,249 @@ +// Narrow file operation for the Inno model choice, also compiled by PowerShell 5.1. +// Example: Run(dataDirectory, 0) previews; Run(dataDirectory, preview.EligibleMask) +// removes only that selection after the installer has obtained explicit consent. +using System; +using System.Collections.Generic; +using System.ComponentModel; +using System.Diagnostics; +using System.Globalization; +using System.IO; +using System.Runtime.InteropServices; +using System.Security.Cryptography; +using System.Text; +using System.Threading; +using Microsoft.Win32.SafeHandles; + +namespace OpenClaw.Shared.Audio +{ + /// Measured catalog files, not a recursive app-data or shared-cache inventory. + public sealed class KokoroUninstallReport + { + public int EligibleMask { get; internal set; } + public long VerifiedBytes { get; internal set; } + public long RetainedBytes { get; internal set; } + public long RemovedBytes { get; internal set; } + public bool Complete { get; internal set; } + public string Text { get; internal set; } + + internal KokoroUninstallReport() { Complete = true; Text = string.Empty; } + } + + /// Verifies catalog contents and deletes by the same locked handle, never recursively. + public static class KokoroUninstallFiles + { + private const int DefaultScanTimeoutSeconds = 120; + private const uint GenericRead = 0x80000000, DeleteAccess = 0x10000; + private const uint ShareRead = 1, OpenExisting = 3; + private const uint OpenReparsePoint = 0x00200000, BackupSemantics = 0x02000000; + private const uint DirectoryAttribute = 0x10, ReparseAttribute = 0x400; + + /// Zero selection is read-only. Bits select only the fixed, shipped catalog. + public static KokoroUninstallReport Run(string dataDirectory, int selectionMask) + { + int seconds; + if (!int.TryParse(Environment.GetEnvironmentVariable("OPENCLAW_MODEL_UNINSTALL_TIMEOUT_SECONDS"), out seconds) + || seconds < 10 || seconds > 600) seconds = DefaultScanTimeoutSeconds; + return Inspect(dataDirectory, KokoroDownloadCatalog.All, selectionMask, + CancellationToken.None, TimeSpan.FromSeconds(seconds)); + } + + internal static KokoroUninstallReport Inspect(string dataDirectory, KokoroDownloadAsset[] assets, + int selectionMask, CancellationToken cancellationToken, TimeSpan timeout) + { + if (assets.Length > 30 || selectionMask < 0 || (selectionMask >> assets.Length) != 0) + throw new ArgumentOutOfRangeException("selectionMask"); + var root = Path.GetFullPath(dataDirectory); + var drive = Path.GetPathRoot(root); + if (root.StartsWith(@"\\", StringComparison.Ordinal) || drive == null || drive.Length != 3) + throw new ArgumentException("Only an ordinary local app-data path can be checked."); + + var report = new KokoroUninstallReport(); + var lines = new StringBuilder(); + var clock = Stopwatch.StartNew(); + for (int i = 0; i < assets.Length; i++) + { + var asset = assets[i]; + // Catalog entries, not caller-provided paths, define the deletion boundary. + if (Path.GetFileName(asset.PackId) != asset.PackId || asset.PackId == ".." + || Path.GetFileName(asset.FileName) != asset.FileName || asset.FileName == "..") + throw new InvalidDataException("Invalid Kokoro catalog path."); + var path = Path.Combine(root, "models", "kokoro", asset.PackId, asset.FileName); + var selected = (selectionMask & (1 << i)) != 0; + long measured = 0; + bool hasMeasurement = false; + var ancestors = new List(); + try + { + CheckBudget(clock, timeout, cancellationToken); + var directory = Path.GetDirectoryName(path); + if (directory == null) throw new IOException("Model directory is unavailable."); + LockAncestors(directory, ancestors); + using (var handle = Open(path, GenericRead | (selected ? DeleteAccess : 0), ShareRead, OpenReparsePoint)) + { + var info = Information(handle); + if ((info.Attributes & (DirectoryAttribute | ReparseAttribute)) != 0 || info.Links != 1) + throw new IOException("Not an ordinary, single-link model file."); + using (var stream = new FileStream(handle, FileAccess.Read)) + { + measured = stream.Length; + hasMeasurement = true; + var digest = Hash(stream, clock, timeout, cancellationToken); + if (!string.Equals(digest, asset.Sha256, StringComparison.OrdinalIgnoreCase)) + { + report.RetainedBytes += measured; + lines.AppendLine("Retained (content not verified): " + path + " (" + Bytes(measured) + ")"); + if (selected) report.Complete = false; + continue; + } + if (selected) + { + CheckBudget(clock, timeout, cancellationToken); + // The handle denies writes/renames; all ancestors deny replacement. + // Hash verification and deletion therefore refer to the same file. + var disposition = new FileDisposition { DeleteFile = true }; + if (!SetFileInformationByHandle(handle, 4, ref disposition, 1)) + throw new Win32Exception(Marshal.GetLastWin32Error()); + } + else + { + report.EligibleMask |= 1 << i; + report.VerifiedBytes += measured; + } + } + } + if (selected) + { + report.RemovedBytes += measured; + lines.AppendLine("Removed: " + path + " (" + Bytes(measured) + ")"); + } + else + { + report.RetainedBytes += measured; + lines.AppendLine("Verified download, kept unless selected: " + path + " (" + Bytes(measured) + ")"); + } + } + catch (Win32Exception ex) + { + if (ex.NativeErrorCode == 2 || ex.NativeErrorCode == 3) + lines.AppendLine("Absent: " + path); + else + RetainFailure(report, lines, path, measured, hasMeasurement, "Windows error " + ex.NativeErrorCode); + } + catch (Exception ex) + { + if (!(ex is IOException) && !(ex is UnauthorizedAccessException) + && !(ex is OperationCanceledException) && !(ex is TimeoutException)) throw; + RetainFailure(report, lines, path, measured, hasMeasurement, + ex is OperationCanceledException ? "cancelled" : + ex is TimeoutException ? "scan deadline reached" : "could not safely verify or remove"); + } + finally + { + for (int j = ancestors.Count - 1; j >= 0; j--) ancestors[j].Dispose(); + } + } + + report.Text = "Verified downloads available for selection: " + Bytes(report.VerifiedBytes) + Environment.NewLine + + "Removed model file bytes: " + Bytes(report.RemovedBytes) + Environment.NewLine + + "Measured retained catalog files: " + Bytes(report.RetainedBytes) + + (report.Complete ? "." : " (incomplete; some files could not be checked or removed).") + + Environment.NewLine + Environment.NewLine + lines + + Environment.NewLine + "Other files and directories are retained; their sizes were not scanned." + + Environment.NewLine + "Retained data root: " + root + + Environment.NewLine + "This choice does not remove settings.json, mcp-token.txt, gateways.json, gateways, other models, partial downloads, or shared caches." + + Environment.NewLine + "Gateway removal is a separate choice and may remove its own credentials and state."; + return report; + } + + private static string Bytes(long value) { return value.ToString("N0", CultureInfo.CurrentCulture) + " bytes"; } + + private static void RetainFailure(KokoroUninstallReport report, StringBuilder lines, string path, + long measured, bool hasMeasurement, string reason) + { + report.Complete = false; + if (hasMeasurement) report.RetainedBytes += measured; + lines.AppendLine("Retained / not verified: " + path + " (" + + (hasMeasurement ? Bytes(measured) + "; " : "size unknown; ") + reason + ")"); + } + + private static void CheckBudget(Stopwatch clock, TimeSpan timeout, CancellationToken token) + { + token.ThrowIfCancellationRequested(); + if (clock.Elapsed >= timeout) throw new TimeoutException(); + } + + private static string Hash(Stream stream, Stopwatch clock, TimeSpan timeout, CancellationToken token) + { + using (var sha = SHA256.Create()) + { + var buffer = new byte[81920]; + int count; + while ((count = stream.Read(buffer, 0, buffer.Length)) != 0) + { + CheckBudget(clock, timeout, token); + sha.TransformBlock(buffer, 0, count, buffer, 0); + } + sha.TransformFinalBlock(new byte[0], 0, 0); + var digest = sha.Hash; + if (digest == null) throw new IOException("Model hash is unavailable."); + return BitConverter.ToString(digest).Replace("-", ""); + } + } + + private static void LockAncestors(string directory, List handles) + { + var paths = new Stack(); + for (var path = directory; !string.IsNullOrEmpty(path); path = Path.GetDirectoryName(path) ?? string.Empty) paths.Push(path); + // Deny write as well as delete sharing: an otherwise pinned directory + // must not become a reparse point before its next descendant is opened. + // Request read access so Windows participates in data-access sharing checks; + // an attribute-only/zero-access handle does not establish this fence. + while (paths.Count != 0) + { + var handle = Open(paths.Pop(), GenericRead, ShareRead, OpenReparsePoint | BackupSemantics); + handles.Add(handle); + var info = Information(handle); + if ((info.Attributes & ReparseAttribute) != 0 || (info.Attributes & DirectoryAttribute) == 0) + throw new IOException("An ancestor is redirected or is not a directory."); + } + } + + private static SafeFileHandle Open(string path, uint access, uint share, uint flags) + { + var handle = CreateFile(path, access, share, IntPtr.Zero, OpenExisting, flags, IntPtr.Zero); + if (!handle.IsInvalid) return handle; + int error = Marshal.GetLastWin32Error(); + handle.Dispose(); + throw new Win32Exception(error); + } + + private static FileInformation Information(SafeFileHandle handle) + { + FileInformation info; + if (!GetFileInformationByHandle(handle, out info)) throw new Win32Exception(Marshal.GetLastWin32Error()); + return info; + } + + [StructLayout(LayoutKind.Sequential)] + private struct FileInformation + { + internal uint Attributes; + internal System.Runtime.InteropServices.ComTypes.FILETIME Creation, Access, Write; + internal uint Volume, SizeHigh, SizeLow, Links, IndexHigh, IndexLow; + } + + [StructLayout(LayoutKind.Sequential)] + private struct FileDisposition { [MarshalAs(UnmanagedType.U1)] internal bool DeleteFile; } + + [DllImport("kernel32.dll", CharSet = CharSet.Unicode, SetLastError = true, EntryPoint = "CreateFileW")] + private static extern SafeFileHandle CreateFile(string path, uint access, uint share, IntPtr security, + uint creation, uint flags, IntPtr template); + [DllImport("kernel32.dll", SetLastError = true)] + [return: MarshalAs(UnmanagedType.Bool)] + private static extern bool GetFileInformationByHandle(SafeFileHandle file, out FileInformation info); + [DllImport("kernel32.dll", SetLastError = true)] + [return: MarshalAs(UnmanagedType.Bool)] + private static extern bool SetFileInformationByHandle(SafeFileHandle file, int informationClass, + ref FileDisposition info, uint size); + } +} diff --git a/tests/OpenClaw.Tray.Tests/InstallerModelCleanupContractTests.cs b/tests/OpenClaw.Tray.Tests/InstallerModelCleanupContractTests.cs new file mode 100644 index 0000000000..0aa3bbbf26 --- /dev/null +++ b/tests/OpenClaw.Tray.Tests/InstallerModelCleanupContractTests.cs @@ -0,0 +1,78 @@ +namespace OpenClaw.Tray.Tests; + +// Inno admission and packaging are distinct from the native file-operation tests. +public sealed class InstallerModelCleanupContractTests +{ + private static string Installer => File.ReadAllText( + Path.Combine(TestRepositoryPaths.GetRepositoryRoot(), "installer.iss")).Replace("\r\n", "\n"); + + [Fact] + public void ModelChoice_DefaultsToKeep_AndSilentUninstallCannotOptIn() + { + var source = Installer; + var choice = source[source.IndexOf("function ChooseModelCleanup: Boolean;\nbegin", StringComparison.Ordinal)..]; + choice = choice[..choice.IndexOf("procedure RunSelectedModelCleanup;", StringComparison.Ordinal)]; + Assert.Contains("ModelCleanupSelection := 0;", choice); + Assert.True(choice.IndexOf("ModelCleanupSelection := 0;", StringComparison.Ordinal) + < choice.IndexOf("if UninstallSilent() then Exit;", StringComparison.Ordinal)); + Assert.DoesNotContain("StartModelHelper", choice); + Assert.DoesNotContain("CheckCompletedStoreMigration", choice); + Assert.Contains("Result := ShowModelReport(True);", choice); + Assert.Contains("ModelRemoveCheck.Checked := False;", source); + Assert.Contains("if Choosing and Result and ModelRemoveCheck.Checked then", source); + Assert.Contains("ModelScanButton.OnClick := @CheckModelPreview;", source); + Assert.Contains("CancelButton.OnClick := @CancelModelPreview;", source); + Assert.Contains("CanClose := not ModelScanRunning;", source); + Assert.Contains("if ModelScanCancelled then ModelDialog.Close;", source); + Assert.DoesNotContain("ModelDialog.ModalResult", source); + Assert.Contains("Result := ChooseModelCleanup;", source); // Cancel aborts admission. + } + + [Fact] + public void ModelRemoval_RechecksPreservation_AndDoesNotDependOnGatewayRemoval() + { + var source = Installer; + var cleanup = source[source.IndexOf("procedure RunSelectedModelCleanup;", StringComparison.Ordinal)..]; + cleanup = cleanup[..cleanup.IndexOf("procedure EnsureLocalGatewayCleanupChoice;", StringComparison.Ordinal)]; + Assert.Contains("if ModelCleanupSelection = 0 then Exit;", cleanup); + Assert.Contains("if MigrationOperationUnavailable then", cleanup); + Assert.True(cleanup.IndexOf("CheckCompletedStoreMigration <> 0", StringComparison.Ordinal) + < cleanup.IndexOf("StartModelHelper(ModelCleanupSelection, ewWaitUntilTerminated", StringComparison.Ordinal)); + Assert.DoesNotContain("LocalGatewayCleanupRequested", cleanup); + Assert.DoesNotContain("LocalGatewayCleanupSucceeded", cleanup); + Assert.DoesNotContain("DelTree", cleanup); + Assert.Contains("ShowModelReport(False);", source); + // Only an atomic completion receipt can authorize the preview mask. + Assert.Contains("if not ReadModelCompletion(Code) then Code := 50;", source); + Assert.Contains("else if (Code >= 32) and (Code <= 47) then", source); + Assert.Contains("ModelPreviewEligible := Code - 32;", source); + Assert.DoesNotContain("model-selection.txt", source); + Assert.DoesNotContain("-SelectionPath", source); + } + + [Fact] + public void Preview_ObservesCompletionWithoutDisablingItsCancelControls() + { + var source = Installer; + var preview = source[source.IndexOf("procedure CheckModelPreview", StringComparison.Ordinal)..]; + preview = preview[..preview.IndexOf("function ShowModelReport", StringComparison.Ordinal)]; + Assert.Contains("StartModelHelper(0, ewNoWait, Code)", preview); + Assert.DoesNotContain("ewWaitUntilTerminated", preview); + Assert.DoesNotContain("CheckCompletedStoreMigration", preview); + Assert.Contains("CreateCallback(@ModelPreviewTick)", preview); + Assert.Contains("Inc(ModelInvocation);", source); + Assert.Contains("if (Timer <> ModelPreviewTimer) or not ModelScanRunning then Exit;", source); + Assert.Contains("finally\n StopModelPreviewTimer;\n ModelDialog.Free;", source); + } + + [Theory] + [InlineData("scripts\\Uninstall-DownloadedModels.ps1", "Uninstall-DownloadedModels.ps1")] + [InlineData("src\\OpenClaw.Shared\\Audio\\KokoroDownloadCatalog.cs", "KokoroDownloadCatalog.cs")] + [InlineData("src\\OpenClaw.Shared\\Audio\\KokoroUninstallFiles.cs", "KokoroUninstallFiles.cs")] + public void ModelHelper_ShipsAuthoritativeSources_AndStagesBeforePayloadRemoval(string sourcePath, string name) + { + var source = Installer; + Assert.Contains($"Source: \"{sourcePath}\"; DestDir: \"{{app}}\"; Flags: ignoreversion", source); + Assert.Contains($"ExpandConstant('{{tmp}}\\{name}')", source); + } +} diff --git a/tests/OpenClaw.Tray.Tests/KokoroUninstallFilesTests.cs b/tests/OpenClaw.Tray.Tests/KokoroUninstallFilesTests.cs new file mode 100644 index 0000000000..434fdb2f4e --- /dev/null +++ b/tests/OpenClaw.Tray.Tests/KokoroUninstallFilesTests.cs @@ -0,0 +1,276 @@ +using System.Diagnostics; +using System.Runtime.InteropServices; +using OpenClaw.Shared.Audio; +using OpenClaw.TestSupport; + +namespace OpenClaw.Tray.Tests; + +public sealed class KokoroUninstallFilesTests +{ + // SHA-256 of the independent three-byte fixture "abc". Catalog size deliberately differs. + private const string FixtureHash = "ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad"; + private static readonly KokoroDownloadAsset[] Assets = + [ + new("fixture", "model.onnx", "https://example.invalid/model", FixtureHash, 999), + new("fixture", "voices.bin", "https://example.invalid/voices", FixtureHash, 999), + ]; + private static readonly TimeSpan TestTimeout = TimeSpan.FromSeconds( + int.TryParse(Environment.GetEnvironmentVariable("OPENCLAW_MODEL_UNINSTALL_TEST_TIMEOUT_SECONDS"), out var seconds) + && seconds > 0 ? seconds : 120); + + [Fact] + public void Preview_MeasuresFilesRatherThanCatalog_AndPreservesEverything() + { + using var temp = new TempDirectory("kokoro-uninstall-"); + var path = Seed(temp, 0); + var unknown = Path.Combine(Path.GetDirectoryName(path)!, "personal.onnx"); + File.WriteAllText(unknown, "unrelated"); + File.WriteAllText(temp.Combine("settings.json"), "settings"); + + var result = Inspect(temp, 0); + + Assert.True(result.Complete); + Assert.Equal(1, result.EligibleMask); + Assert.Equal(3, result.VerifiedBytes); + Assert.Equal(3, result.RetainedBytes); + Assert.Equal(0, result.RemovedBytes); + Assert.Equal("abc", File.ReadAllText(path)); + Assert.Equal("unrelated", File.ReadAllText(unknown)); + Assert.Equal("settings", File.ReadAllText(temp.Combine("settings.json"))); + Assert.Contains(path, result.Text); + Assert.Contains("sizes were not scanned", result.Text); + } + + [Fact] + public void Remove_OnlyPreviewSelection_PreservesUnselectedAndUnrelatedFiles() + { + using var temp = new TempDirectory("kokoro-uninstall-"); + var first = Seed(temp, 0); + var selection = Inspect(temp, 0).EligibleMask; + var later = Seed(temp, 1); // Downloaded after the preview. Not consented. + var unknown = Path.Combine(Path.GetDirectoryName(first)!, "notes.txt"); + File.WriteAllText(unknown, "keep"); + File.WriteAllText(temp.Combine("mcp-token.txt"), "fixture-token"); + + var result = Inspect(temp, selection); + + Assert.True(result.Complete); + Assert.Equal(3, result.RemovedBytes); + Assert.Equal(3, result.RetainedBytes); + Assert.False(File.Exists(first)); + Assert.Equal("abc", File.ReadAllText(later)); + Assert.Equal("keep", File.ReadAllText(unknown)); + Assert.Equal("fixture-token", File.ReadAllText(temp.Combine("mcp-token.txt"))); + Assert.True(Directory.Exists(Path.GetDirectoryName(first))); + } + + [Fact] + public void Remove_RechecksContentAfterPreview_AndReportsChangedFileRetained() + { + using var temp = new TempDirectory("kokoro-uninstall-"); + var path = Seed(temp, 0); + var selection = Inspect(temp, 0).EligibleMask; + File.WriteAllText(path, "user model"); + + var result = Inspect(temp, selection); + + Assert.False(result.Complete); + Assert.Equal(0, result.RemovedBytes); + Assert.Equal(10, result.RetainedBytes); + Assert.Equal("user model", File.ReadAllText(path)); + Assert.Contains("content not verified", result.Text); + } + + [Fact] + public void Remove_MissingSelectedFile_IsNotCountedAsRemoved() + { + using var temp = new TempDirectory("kokoro-uninstall-"); + var path = Seed(temp, 0); + File.Delete(path); + var result = Inspect(temp, 1); + Assert.True(result.Complete); + Assert.Equal(0, result.RemovedBytes); + Assert.Contains("Absent: " + path, result.Text); + } + + [Fact] + public void LockedFile_DoesNotHidePartialScanOrPreventOtherSelectedCleanup() + { + using var temp = new TempDirectory("kokoro-uninstall-"); + var first = Seed(temp, 0); + var second = Seed(temp, 1); + using (var locked = new FileStream(first, FileMode.Open, FileAccess.Read, FileShare.None)) + { + var result = Inspect(temp, 3); + Assert.False(result.Complete); + Assert.Equal(3, result.RemovedBytes); + Assert.Contains("size unknown", result.Text); + Assert.Contains(first, result.Text); + } + Assert.Equal("abc", File.ReadAllText(first)); + Assert.False(File.Exists(second)); + } + + [Fact] + public void ReadOnlyFile_ReportsRemovalFailureWithoutClaimingRemovedBytes() + { + using var temp = new TempDirectory("kokoro-uninstall-"); + var path = Seed(temp, 0); + File.SetAttributes(path, FileAttributes.ReadOnly); + try + { + var result = Inspect(temp, 1); + Assert.False(result.Complete); + Assert.Equal(0, result.RemovedBytes); + Assert.True(File.Exists(path)); + } + finally { File.SetAttributes(path, FileAttributes.Normal); } + } + + [Theory] + [InlineData(true)] + [InlineData(false)] + public void CancellationOrExpiredBudget_PreservesFilesAndMarksIncomplete(bool cancel) + { + using var temp = new TempDirectory("kokoro-uninstall-"); + var path = Seed(temp, 0); + var result = KokoroUninstallFiles.Inspect(temp.Path, Assets, 1, + new CancellationToken(cancel), cancel ? TestTimeout : TimeSpan.Zero); + Assert.False(result.Complete); + Assert.Equal(0, result.RemovedBytes); + Assert.Equal("abc", File.ReadAllText(path)); + Assert.Contains(cancel ? "cancelled" : "deadline", result.Text); + } + + [Fact] + public void HardLinkedCatalogFile_IsRetainedEvenWhenItsContentsMatch() + { + using var temp = new TempDirectory("kokoro-uninstall-"); + var path = Seed(temp, 0); + var other = temp.Combine("personal-copy.onnx"); + Assert.True(CreateHardLink(other, path, IntPtr.Zero), $"Windows error {Marshal.GetLastWin32Error()}"); + var result = Inspect(temp, 1); + Assert.False(result.Complete); + Assert.Equal(0, result.RemovedBytes); + Assert.Equal("abc", File.ReadAllText(path)); + Assert.Equal("abc", File.ReadAllText(other)); + } + + [Fact] + public async Task RedirectedModelAncestor_IsNotTraversedOrDeleted() + { + using var temp = new TempDirectory("kokoro-uninstall-"); + using var external = new TempDirectory("kokoro-unrelated-"); + var path = Seed(external, 0); + var link = temp.Combine("models"); + using var process = Process.Start(new ProcessStartInfo("cmd.exe") + { + Arguments = $"/d /c mklink /J \"{link}\" \"{external.Combine("models")}\"", + UseShellExecute = false, + CreateNoWindow = true, + }); + Assert.NotNull(process); + try { await process.WaitForExitAsync().WaitAsync(TestTimeout); } + finally { if (!process.HasExited) process.Kill(entireProcessTree: true); } + Assert.Equal(0, process.ExitCode); + try + { + var result = Inspect(temp, 1); + Assert.False(result.Complete); + Assert.Equal(0, result.RemovedBytes); + Assert.Equal(0, result.VerifiedBytes); + Assert.Equal("abc", File.ReadAllText(path)); + } + finally { Directory.Delete(link); } + } + + [Fact] + public void PinnedAncestor_DeniesReparseWriterBeforeDescendantIsOpened() + { + using var temp = new TempDirectory("kokoro-ancestor-pin-"); + var ancestor = temp.Combine("models"); + Directory.CreateDirectory(ancestor); + var handles = new List(); + // Exercise the actual native pin boundary without timing a racing thread or + // adding a production callback. The directory is empty, so a reparse writer + // would otherwise be able to redirect the next descendant lookup. + var pin = typeof(KokoroUninstallFiles).GetMethod("LockAncestors", + System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.NonPublic); + Assert.NotNull(pin); + try + { + pin.Invoke(null, [ancestor, handles]); + using var writer = OpenDirectoryWriter(ancestor, 0x40000000, 7, IntPtr.Zero, + 3, 0x02200000, IntPtr.Zero); + var error = Marshal.GetLastWin32Error(); + Assert.True(writer.IsInvalid); + Assert.Equal(32, error); // ERROR_SHARING_VIOLATION, before FSCTL_SET_REPARSE_POINT. + } + finally { foreach (var handle in handles) handle.Dispose(); } + using var afterRelease = OpenDirectoryWriter(ancestor, 0x40000000, 7, IntPtr.Zero, + 3, 0x02200000, IntPtr.Zero); + Assert.False(afterRelease.IsInvalid); // Fixture permits the operation without pins. + } + + [DllImport("kernel32.dll", CharSet = CharSet.Unicode, SetLastError = true, EntryPoint = "CreateFileW")] + private static extern Microsoft.Win32.SafeHandles.SafeFileHandle OpenDirectoryWriter( + string path, uint access, uint share, IntPtr security, uint creation, uint flags, IntPtr template); + + [Fact] + public void InstallerCatalog_CoversExactlyTheDownloadConsumers() + { + var downloads = KokoroModelManager.AvailablePacks.SelectMany(pack => + new[] { pack.Model, pack.VoicesFile }.Select(asset => + (pack.PackId, asset.FileName, asset.DownloadUrl, asset.Sha256, asset.SizeBytes))).ToArray(); + var cleanup = KokoroDownloadCatalog.All.Select(asset => + (asset.PackId, asset.FileName, asset.Url, (string?)asset.Sha256, asset.SizeBytes)).ToArray(); + Assert.Equal(downloads, cleanup); + } + + [Theory] + [InlineData(false)] + [InlineData(true)] + public async Task PowerShell51_RunsPreviewOrHonorsCancellationBeforeStarting(bool cancel) + { + using var temp = new TempDirectory("kokoro-uninstall-host-"); + var root = TestRepositoryPaths.GetRepositoryRoot(); + foreach (var name in new[] { "KokoroDownloadCatalog.cs", "KokoroUninstallFiles.cs" }) + File.Copy(Path.Combine(root, "src", "OpenClaw.Shared", "Audio", name), temp.Combine(name)); + File.Copy(Path.Combine(root, "scripts", "Uninstall-DownloadedModels.ps1"), + temp.Combine("Uninstall-DownloadedModels.ps1")); + var data = temp.Combine("data with spaces"); + Directory.CreateDirectory(data); + var output = await UninstallScriptHost.RunAsync(temp, ["ConvertTo-CleanProcessOutput"], """ + $cancelPath = Join-Path $AppRoot 'cancel.txt' + $expected = 32 + if ($env:OPENCLAW_TEST_CANCEL_MODELS -eq 'True') { + [IO.File]::WriteAllText($cancelPath, 'cancel') + $expected = 49 + } + & (Join-Path $AppRoot 'Uninstall-DownloadedModels.ps1') -DataDirectory (Join-Path $AppRoot 'data with spaces') -ReportPath (Join-Path $AppRoot 'report.txt') -CancellationPath $cancelPath + if ($LASTEXITCODE -ne $expected) { throw "Unexpected model helper result: $LASTEXITCODE" } + Write-Output $LASTEXITCODE + Write-Output ([IO.File]::ReadAllText((Join-Path $AppRoot 'report.txt'))) + """, new Dictionary { ["OPENCLAW_TEST_CANCEL_MODELS"] = cancel.ToString() }); + Assert.StartsWith(cancel ? "49" : "32", output.Trim()); + Assert.Contains(cancel ? "cancelled before starting" : "Absent:", output); + Assert.Contains(data, output); + if (!cancel) Assert.Contains("sizes were not scanned", output); + } + + private static KokoroUninstallReport Inspect(TempDirectory temp, int selection) => + KokoroUninstallFiles.Inspect(temp.Path, Assets, selection, CancellationToken.None, TestTimeout); + + private static string Seed(TempDirectory temp, int index) + { + var directory = temp.Combine("models", "kokoro", "fixture"); + Directory.CreateDirectory(directory); + var path = Path.Combine(directory, Assets[index].FileName); + File.WriteAllText(path, "abc"); + return path; + } + + [DllImport("kernel32.dll", CharSet = CharSet.Unicode, SetLastError = true, EntryPoint = "CreateHardLinkW")] + [return: MarshalAs(UnmanagedType.Bool)] + private static extern bool CreateHardLink(string link, string existing, IntPtr security); +} diff --git a/tests/OpenClaw.Tray.Tests/KokoroUninstallProcessTests.cs b/tests/OpenClaw.Tray.Tests/KokoroUninstallProcessTests.cs new file mode 100644 index 0000000000..f5f8c767f1 --- /dev/null +++ b/tests/OpenClaw.Tray.Tests/KokoroUninstallProcessTests.cs @@ -0,0 +1,160 @@ +using System.Diagnostics; +using OpenClaw.Shared.Audio; +using OpenClaw.TestSupport; + +namespace OpenClaw.Tray.Tests; + +// Exercises the shipped supervisor/worker protocol on Windows, not an Inno UI mock. +public sealed class KokoroUninstallProcessTests +{ + private const string FixtureHash = "ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad"; + private static readonly TimeSpan Timeout = TimeSpan.FromSeconds( + int.TryParse(Environment.GetEnvironmentVariable("OPENCLAW_MODEL_UNINSTALL_TEST_TIMEOUT_SECONDS"), out var seconds) + && seconds > 0 ? seconds : 120); + + [Theory] + [InlineData(0, false, false, 33, true)] + [InlineData(1, true, false, 32, false)] + [InlineData(1, true, true, 48, true)] + [InlineData(1, false, false, 49, true)] + public async Task Completion_PreservesEncodedSelectionAndPartialOutcomes( + int selection, bool confirm, bool readOnly, int expectedCode, bool retained) + { + using var temp = new TempDirectory("kokoro-process-"); + Stage(temp); + // Only catalog input hashes change. Shipped operation and supervisor source + // execute unchanged against real files, without downloading hundreds of MB. + var catalogPath = temp.Combine("KokoroDownloadCatalog.cs"); + var catalog = File.ReadAllText(catalogPath); + foreach (var asset in KokoroDownloadCatalog.All) catalog = catalog.Replace(asset.Sha256, FixtureHash); + File.WriteAllText(catalogPath, catalog); + var model = temp.Combine("data", "models", "kokoro", + KokoroDownloadCatalog.All[0].PackId, KokoroDownloadCatalog.All[0].FileName); + Directory.CreateDirectory(Path.GetDirectoryName(model)!); + File.WriteAllText(model, "abc"); + File.WriteAllText(temp.Combine("data", "settings.json"), "keep"); + if (readOnly) File.SetAttributes(model, FileAttributes.ReadOnly); + try + { + using var process = Start(temp, selection, confirm); + var stdout = process.StandardOutput.ReadToEndAsync(); + var stderr = process.StandardError.ReadToEndAsync(); + try { await process.WaitForExitAsync().WaitAsync(Timeout); } + finally { await StopIfRunning(process); } + Assert.True(process.ExitCode == expectedCode, await stdout + await stderr); + var report = File.ReadAllLines(temp.Combine("report.txt")); + Assert.Equal(expectedCode.ToString(), report[0]); + Assert.True(report.Length > 1); + Assert.False(File.Exists(temp.Combine("report.txt.pending"))); + Assert.Equal(retained, File.Exists(model)); + Assert.Equal("keep", File.ReadAllText(temp.Combine("data", "settings.json"))); + Assert.Contains(confirm || selection == 0 ? model : temp.Combine("data", "models", "kokoro"), string.Join('\n', report)); + if (selection == 0) Assert.Contains("3 bytes", string.Join('\n', report)); + if (readOnly) Assert.Contains("incomplete", string.Join('\n', report)); + } + finally { if (File.Exists(model)) File.SetAttributes(model, FileAttributes.Normal); } + } + + [Fact] + public async Task Cancellation_PublishesOnlyAfterTheRunningWorkerReleasesItsFile() + { + using var temp = new TempDirectory("kokoro-cancel-"); + Stage(temp); + Directory.CreateDirectory(temp.Combine("data")); + // Controlled external worker for the supervisor's cancellation boundary. + // This is not file-cleanup or real-installer proof: the real-worker cases above + // cover file operations. No test-only switches enter production code. + File.WriteAllText(temp.Combine("KokoroUninstallFiles.cs"), """ + using System; + using System.IO; + using System.Threading; + namespace OpenClaw.Shared.Audio { + public static class KokoroUninstallFiles { + public static object Run(string root, int selection) { + using (var held = new FileStream(Path.Combine(root, "held"), FileMode.CreateNew, FileAccess.Write, FileShare.None)) { + File.WriteAllText(Path.Combine(root, "ready"), "ready"); + // Self-bounded even if the supervisor regresses or its test crashes. + using (var release = new ManualResetEvent(false)) + release.WaitOne(int.Parse(Environment.GetEnvironmentVariable("OPENCLAW_TEST_STALLED_WORKER_TIMEOUT_MS"))); + } + return null; + } + } + } + """); + var ready = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously); + using var watcher = new FileSystemWatcher(temp.Combine("data"), "ready"); + watcher.Created += (_, _) => ready.TrySetResult(true); + watcher.EnableRaisingEvents = true; + var receipt = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously); + using var reports = new FileSystemWatcher(temp.Path, "report.txt"); + void ObserveReceipt() + { + try + { + using var released = new FileStream(temp.Combine("data", "held"), FileMode.Open, FileAccess.ReadWrite, FileShare.None); + receipt.TrySetResult(null); + } + catch (Exception error) { receipt.TrySetResult(error); } + } + reports.Created += (_, _) => ObserveReceipt(); + reports.Renamed += (_, _) => ObserveReceipt(); + reports.EnableRaisingEvents = true; + using var process = Start(temp, 0, false); + var stdout = process.StandardOutput.ReadToEndAsync(); + var stderr = process.StandardError.ReadToEndAsync(); + try + { + var exit = process.WaitForExitAsync(); + var first = await Task.WhenAny(ready.Task, exit).WaitAsync(Timeout); + Assert.True(first == ready.Task, first == exit ? await stdout + await stderr : "Worker did not become ready."); + Assert.False(File.Exists(temp.Combine("report.txt"))); // No premature completion. + File.WriteAllText(temp.Combine("cancel.txt"), "cancel"); + // Check the lock when completion becomes observable, before awaiting + // supervisor exit. An early published receipt must fail this assertion. + Assert.Null(await receipt.Task.WaitAsync(Timeout)); + await exit.WaitAsync(Timeout); + Assert.True(process.ExitCode == 49, await stdout + await stderr); + var report = File.ReadAllLines(temp.Combine("report.txt")); + Assert.Equal("49", report[0]); + Assert.Contains("cancelled", string.Join('\n', report)); + } + finally { await StopIfRunning(process); } + } + + private static void Stage(TempDirectory temp) + { + var root = TestRepositoryPaths.GetRepositoryRoot(); + File.Copy(Path.Combine(root, "scripts", "Uninstall-DownloadedModels.ps1"), temp.Combine("Uninstall-DownloadedModels.ps1")); + foreach (var name in new[] { "KokoroDownloadCatalog.cs", "KokoroUninstallFiles.cs" }) + File.Copy(Path.Combine(root, "src", "OpenClaw.Shared", "Audio", name), temp.Combine(name)); + } + + private static Process Start(TempDirectory temp, int selection, bool confirm) + { + var powershell = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Windows), + "System32", "WindowsPowerShell", "v1.0", "powershell.exe"); + var start = new ProcessStartInfo(powershell) + { + UseShellExecute = false, CreateNoWindow = true, + RedirectStandardOutput = true, RedirectStandardError = true, + WorkingDirectory = temp.Path, + }; + foreach (var argument in new[] { + "-NoProfile", "-NonInteractive", "-ExecutionPolicy", "Bypass", "-File", temp.Combine("Uninstall-DownloadedModels.ps1"), + "-DataDirectory", temp.Combine("data"), "-ReportPath", temp.Combine("report.txt"), + "-CancellationPath", temp.Combine("cancel.txt"), "-SelectionMask", selection.ToString(), + }) start.ArgumentList.Add(argument); + start.Environment["OPENCLAW_TEST_STALLED_WORKER_TIMEOUT_MS"] = + checked((int)Timeout.TotalMilliseconds).ToString(System.Globalization.CultureInfo.InvariantCulture); + if (confirm) start.ArgumentList.Add("-ConfirmDestructive"); + return Process.Start(start) ?? throw new InvalidOperationException("Could not start the model supervisor."); + } + + private static async Task StopIfRunning(Process process) + { + if (process.HasExited) return; + process.Kill(entireProcessTree: true); + await process.WaitForExitAsync().WaitAsync(Timeout); + } +}