Repository navigation
Expand file tree
/
Copy pathuninstall.ps1
More file actions
275 lines (246 loc) · 10 KB
/
Copy pathuninstall.ps1
File metadata and controls
275 lines (246 loc) · 10 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
# SPDX-FileCopyrightText: Copyright (c) 2026, NVIDIA CORPORATION & AFFILIATES. All rights reserved.
# SPDX-License-Identifier: Apache-2.0
[CmdletBinding()]
param(
[string]$InstallDir,
[switch]$DryRun,
[switch]$Force,
[switch]$Help
)
$ErrorActionPreference = 'Stop'
function Show-Usage {
@'
Remove the NeMo Relay CLI installed by install.ps1.
Usage:
irm https://raw.githubusercontent.com/NVIDIA/NeMo-Relay/main/uninstall.ps1 | iex
.\uninstall.ps1 [-InstallDir DIR] [-DryRun] [-Force] [-Help]
Options:
-InstallDir DIR Installation directory (default: %LOCALAPPDATA%\nemo-relay\bin).
-DryRun Print the binary that would be removed without removing it.
-Force Interactively offer to terminate active Relay client processes.
-Help Show this help text.
Examples:
irm https://raw.githubusercontent.com/NVIDIA/NeMo-Relay/main/uninstall.ps1 | iex
.\uninstall.ps1 -InstallDir "$HOME\bin"
iwr https://raw.githubusercontent.com/NVIDIA/NeMo-Relay/main/uninstall.ps1 -OutFile uninstall.ps1
.\uninstall.ps1 -Force
This removes only the installed CLI binary. It does not remove PATH entries,
Relay configuration, observability output, or coding-agent integrations. It
refuses removal while this CLI has active Relay processes unless -Force is used.
Active managed daemon processes always block removal, including with -Force.
Close affected coding-agent sessions, allow shared workers to drain, then stop
the managed deployment through its service manager. Managed bundles, daemon
identity/trust state, and deployment services are preserved.
'@ | Write-Output
}
function Fail([string]$Message) {
throw "nemo-relay uninstaller: $Message"
}
function Test-CodingAgentProcess([string]$CommandLine) {
return $CommandLine -match '(?i)(?:^|\s|[\\/])(codex|claude|pi)(?:\.exe)?(?:\s|$|[\\/])'
}
function Test-RelayMcpProcess([string]$CommandLine) {
return $CommandLine -match '(?i)(?:^|\s)mcp(?:\s|$)'
}
function Get-RelayCimInstance([string]$ClassName, [string]$Filter) {
if ([string]::IsNullOrEmpty($Filter)) {
return Get-CimInstance -ClassName $ClassName
}
return Get-CimInstance -ClassName $ClassName -Filter $Filter
}
function Read-UninstallConfirmation([string]$Prompt) {
return Read-Host $Prompt
}
function Get-ActiveRelayShutdownTargets([string]$Destination, [switch]$ManagedOnly) {
if ($env:OS -ne 'Windows_NT') {
return @()
}
try {
$processes = @(Get-RelayCimInstance Win32_Process)
}
catch {
Fail "could not inspect active processes before uninstall: $($_.Exception.Message)"
}
$processesById = @{}
foreach ($process in $processes) {
$processesById[[string]$process.ProcessId] = $process
}
$targets = @{}
$expectedName = [System.IO.Path]::GetFileName($Destination)
foreach ($relayProcess in $processes) {
$commandLine = [string]$relayProcess.CommandLine
$executablePath = [string]$relayProcess.ExecutablePath
$hasExpectedName = [string]::Equals($relayProcess.Name, $expectedName, [System.StringComparison]::OrdinalIgnoreCase)
$usesInstalledCli = $executablePath.Equals($Destination, [System.StringComparison]::OrdinalIgnoreCase) -or
($hasExpectedName -and $commandLine.IndexOf($Destination, [System.StringComparison]::OrdinalIgnoreCase) -ge 0)
if (-not $usesInstalledCli) {
continue
}
if ($ManagedOnly) {
if ($commandLine -match '(?i)(?:^|\s)daemon(?:\s|$)') {
$targets[[string]$relayProcess.ProcessId] = $relayProcess
}
continue
}
$target = $relayProcess
if (Test-RelayMcpProcess $commandLine) {
$currentId = [string]$relayProcess.ParentProcessId
while ($currentId -and $currentId -ne '0') {
$current = $processesById[$currentId]
if ($null -eq $current) {
break
}
if (-not [string]::IsNullOrEmpty($current.CommandLine) -and (Test-CodingAgentProcess $current.CommandLine)) {
$target = $current
break
}
$nextId = [string]$current.ParentProcessId
if ($nextId -eq $currentId) {
break
}
$currentId = $nextId
}
}
$targets[[string]$target.ProcessId] = $target
}
return @($targets.Values)
}
function Test-SameProcessIdentity($Expected, $Current) {
return $Expected.ProcessId -eq $Current.ProcessId -and
$Expected.ParentProcessId -eq $Current.ParentProcessId -and
[string]$Expected.CreationDate -eq [string]$Current.CreationDate -and
[string]::Equals(
[string]$Expected.ExecutablePath,
[string]$Current.ExecutablePath,
[System.StringComparison]::OrdinalIgnoreCase
)
}
function Test-ActiveRelayShutdownTarget($ExpectedProcess, [string]$Destination) {
foreach ($current in @(Get-ActiveRelayShutdownTargets $Destination)) {
if ($current.ProcessId -eq $ExpectedProcess.ProcessId) {
return Test-SameProcessIdentity $ExpectedProcess $current
}
}
return $false
}
function Format-ProcessDescription($Process) {
if ([string]::IsNullOrEmpty($Process.CommandLine)) {
return "PID $($Process.ProcessId)"
}
return "PID $($Process.ProcessId): $($Process.CommandLine)"
}
function Test-ProcessIsAncestorOfUninstaller([uint32]$AncestorId) {
$currentId = [uint32]$PID
while ($currentId -gt 0) {
if ($currentId -eq $AncestorId) {
return $true
}
try {
$current = Get-RelayCimInstance Win32_Process -Filter "ProcessId = $currentId"
}
catch {
Fail "could not inspect the uninstaller process tree: $($_.Exception.Message)"
}
if ($null -eq $current -or $current.ParentProcessId -eq $currentId) {
return $false
}
$currentId = [uint32]$current.ParentProcessId
}
return $false
}
function Stop-ConfirmedProcessTree($Process, [string]$Destination) {
if (Test-ProcessIsAncestorOfUninstaller $Process.ProcessId) {
Fail "cannot terminate process $($Process.ProcessId) from within its own process tree; rerun the uninstaller from an independent terminal"
}
$answer = Read-UninstallConfirmation "Terminate $(Format-ProcessDescription $Process) and its child processes? [y/N]"
if ($answer -notmatch '^(?i:y|yes)$') {
Fail "uninstall cancelled; process $($Process.ProcessId) remains active"
}
if (-not (Test-ActiveRelayShutdownTarget $Process $Destination)) {
Fail "process $($Process.ProcessId) changed after confirmation; refusing to terminate it"
}
Assert-NoManagedRelayProcesses $Destination
& taskkill.exe /PID $Process.ProcessId /T /F | Out-Null
if ($LASTEXITCODE -ne 0) {
Fail "could not terminate process $($Process.ProcessId)"
}
}
function Assert-NoManagedRelayProcesses([string]$Destination) {
# Managed workers can serve sessions outside their local process tree.
$managedProcesses = @(Get-ActiveRelayShutdownTargets $Destination -ManagedOnly)
if ($managedProcesses.Count -gt 0) {
foreach ($process in $managedProcesses) {
[Console]::Error.WriteLine((Format-ProcessDescription $process))
}
$message = 'active managed daemon deployment; close affected coding-agent sessions, allow shared workers to drain, then stop it through its service manager. -Force cannot override this restriction'
if ($DryRun) {
Fail "dry run would refuse removal: $message"
}
Fail $message
}
}
function Stop-ActiveRelayProcesses([string]$Destination) {
Assert-NoManagedRelayProcesses $Destination
$activeTargets = @(Get-ActiveRelayShutdownTargets $Destination)
if ($activeTargets.Count -eq 0) {
return
}
[Console]::Error.WriteLine('Active Relay processes prevent uninstallation:')
foreach ($process in $activeTargets) {
[Console]::Error.WriteLine((Format-ProcessDescription $process))
}
if ($DryRun) {
Fail 'dry run would refuse removal until these processes exit'
}
if (-not $Force) {
Fail 'refusing to uninstall while active Relay processes exist; close the coding agents and retry, or rerun with -Force to confirm each process shutdown'
}
foreach ($process in $activeTargets) {
Stop-ConfirmedProcessTree $process $Destination
}
for ($attempt = 0; $attempt -lt 25; $attempt++) {
if (@(Get-ActiveRelayShutdownTargets $Destination).Count -eq 0) {
return
}
Start-Sleep -Milliseconds 200
}
Fail 'refusing to uninstall because Relay processes remain active'
}
if ($Help) {
Show-Usage
exit 0
}
try {
if ([string]::IsNullOrWhiteSpace($InstallDir)) {
if ([string]::IsNullOrWhiteSpace($env:LOCALAPPDATA)) {
Fail 'LOCALAPPDATA must be set to choose the default install directory'
}
$InstallDir = Join-Path $env:LOCALAPPDATA 'nemo-relay\bin'
}
if ([string]::IsNullOrWhiteSpace($InstallDir)) {
Fail 'install directory must not be empty'
}
if ((Test-Path -LiteralPath $InstallDir) -and -not (Test-Path -LiteralPath $InstallDir -PathType Container)) {
Fail "install path is not a directory: $InstallDir"
}
$InstallDir = [System.IO.Path]::GetFullPath($InstallDir)
$destination = Join-Path $InstallDir 'nemo-relay.exe'
if (Test-Path -LiteralPath $destination -PathType Container) {
Fail "install target is a directory: $destination"
}
if (-not (Test-Path -LiteralPath $destination -PathType Leaf)) {
Write-Output "NeMo Relay CLI is not installed at $destination"
exit 0
}
Stop-ActiveRelayProcesses $destination
if ($DryRun) {
Write-Output "Would remove NeMo Relay CLI at $destination"
exit 0
}
Remove-Item -LiteralPath $destination -Force
Write-Output "Removed NeMo Relay CLI from $destination"
}
catch {
Write-Error $_
exit 1
}