$ErrorActionPreference = 'Stop' $projectRoot = Split-Path -Parent $MyInvocation.MyCommand.Path $statePath = Join-Path $projectRoot '.runtime\server.json' if (-not (Test-Path -LiteralPath $statePath)) { Write-Output 'No managed server.'; exit 0 } $state = Get-Content -LiteralPath $statePath -Raw | ConvertFrom-Json $process = Get-CimInstance Win32_Process -Filter "ProcessId=$($state.pid)" -ErrorAction SilentlyContinue $expectedPython = Join-Path $projectRoot '.venv\Scripts\python.exe' if ($process) { if ($state.root -ne $projectRoot -or $process.ExecutablePath -ne $expectedPython -or $process.CommandLine -notmatch '-m\s+studio\s+--port\s+\d+') { throw 'Process identity does not match this project; no process was stopped.' } Stop-Process -Id $state.pid -Force } Remove-Item -LiteralPath $statePath Write-Output 'Kaoyan Studio stopped. Saved data is retained.'