<#
  adom-browser-extension :: AD-safety neutralize + verify
  -------------------------------------------------------
  Run this LOCALLY on the Windows laptop (NOT through adom-desktop) BEFORE re-opening AD, after the
  2026-06-19 port-exhaustion incident. No admin / UAC needed — it only touches HKCU + the user profile.

  It ONLY touches adom-browser-extension artifacts:
    - the native-messaging host registration named 'inc.adom.native_browser' (Chrome + Edge)
    - the 'native-browser' bridge dir in AD's bridges-cache
    - the bridge discovery/pidfiles under ~/.adom/bridges/native-browser
    - node.exe processes running native-host.js
  Nothing else on the machine is read or modified.

  Removing the host registration + the cached bridge makes the runaway loop PHYSICALLY impossible:
  Chrome/Edge can no longer spawn the host, and AD can no longer auto-spawn the bridge.

  Usage (PowerShell, normal window — no admin):
    powershell -ExecutionPolicy Bypass -File ad-safety-check.ps1
#>
$ErrorActionPreference = 'SilentlyContinue'
$HostName = 'inc.adom.native_browser'
$pending  = 0
Write-Host "=== adom-browser-extension safety check  $(Get-Date) ===" -ForegroundColor Cyan

# 1) KILL-SWITCH: native-messaging host registration (Chrome + Edge).
#    Without this, connectNative() cannot launch a host -> the loop cannot start.
foreach ($v in 'Google\Chrome','Microsoft\Edge') {
  $key = "HKCU:\Software\$v\NativeMessagingHosts\$HostName"
  if (Test-Path $key) {
    Write-Host "[FOUND]  host registration: $key  -> removing" -ForegroundColor Yellow
    Remove-Item $key -Force
  } else { Write-Host "[clean]  no host registration under $v" -ForegroundColor Green }
}

# 2) Bridge files in AD's cache (so AD cannot auto-spawn the broken bridge on startup).
$cache = Join-Path $env:LOCALAPPDATA 'Adom Desktop\bridges-cache\native-browser'
if (Test-Path $cache) {
  Write-Host "[FOUND]  bridge in cache: $cache  -> removing" -ForegroundColor Yellow
  Remove-Item $cache -Recurse -Force
} else { Write-Host "[clean]  no native-browser bridge in bridges-cache" -ForegroundColor Green }

# 3) Discovery file + pidfile (host.json / host.pid).
$bdir = Join-Path $env:USERPROFILE '.adom\bridges\native-browser'
if (Test-Path $bdir) {
  Write-Host "[FOUND]  discovery dir: $bdir  -> removing" -ForegroundColor Yellow
  Get-ChildItem $bdir | ForEach-Object { Write-Host "           $($_.Name)" }
  Remove-Item $bdir -Recurse -Force
} else { Write-Host "[clean]  no discovery/pidfiles" -ForegroundColor Green }

# 4) Running looping hosts (node.exe -> native-host.js).
$hosts = Get-CimInstance Win32_Process -Filter "Name='node.exe'" | Where-Object { $_.CommandLine -like '*native-host.js*' }
if ($hosts) {
  Write-Host "[FOUND]  $($hosts.Count) native-host process(es) running -> killing" -ForegroundColor Yellow
  $hosts | ForEach-Object { Write-Host "           pid $($_.ProcessId)"; Stop-Process -Id $_.ProcessId -Force }
} else { Write-Host "[clean]  no native-host.js processes running" -ForegroundColor Green }

# 5) Socket health (did the machine recover from the WSAENOBUFS / 10055 exhaustion?).
$tcp  = (Get-NetTCPConnection).Count
$node = (Get-CimInstance Win32_Process -Filter "Name='node.exe'").Count
Write-Host "[info]   total TCP connections: $tcp   (healthy idle machine is typically a few hundred)"
Write-Host "[info]   total node.exe processes: $node"

Write-Host "`n=== RESULT ===" -ForegroundColor Cyan
Write-Host "All [clean]/[info]  ->  SAFE to open AD:" -ForegroundColor Green
Write-Host "  * Chrome/Edge can no longer spawn the host (registration removed)"
Write-Host "  * AD can no longer auto-spawn the bridge (cache removed)"
Write-Host "Open AD, let it reconnect to galliaApril, then tell Claude 'AD is up'."
