<# AHAN Zero-Install Windows Seeker Bootstrap (dirty-host proxy/encoding/stub shielded). Works in pure Windows PowerShell 5.1+ with ZERO Python installed for -Action scan, guide, and search, and expands Python discovery beyond PATH (skipping Microsoft Store WindowsApps stubs) with structured JSON guidance or opt-in -AutoBootstrapPython when Python is required. Canonical 1-line entry (uses curl.exe --noproxy '*', avoids PowerShell web cmdlets): powershell.exe -NoProfile -ExecutionPolicy Bypass -Command "$f=Join-Path $env:TEMP 'ahan-seeker.ps1'; curl.exe -fsSL --noproxy '*' 'https://ahanet.si/seeker.ps1' -o $f; if($LASTEXITCODE -eq 0){ & $f -Server 'https://ahanet.si' }" #> param( [string]$Server = 'https://ahanet.si', [string]$WorkDir = "", [string]$Action = "scan", [string]$Profile = "", [string]$CredentialFile = "", [string]$HomeDir = "", [string]$WorkId = "", [string]$ToolTarget = "local-dev-env", [string]$OutDir = "", [string]$IdempotencyKey = "", [string[]]$ProbeBin = @(), [switch]$SkipDownload, [switch]$InsecureTls, [switch]$AutoBootstrapPython, [string]$BootstrapPythonUrl = "https://registry.npmmirror.com/-/binary/python/3.12.10/python-3.12.10-embed-amd64.zip", [string]$BootstrapPythonSha256 = "642047692622352c444517a7a690517a7e0e6e720b9f6e595a638f73f27d29c6" ) $ErrorActionPreference = "Stop" # 1. Validate origin & neutralize dirty-host console encoding and dead proxy traps (process-scoped ONLY) $Server = $Server.Trim().TrimEnd('/') $uri = $null if (-not ([System.Uri]::TryCreate($Server, [System.UriKind]::Absolute, [ref]$uri)) -or ($uri.Scheme -notin @("http", "https"))) { throw "Invalid -Server URL '$Server'. Pass an explicit http:// or https:// AHAN origin." } $env:PYTHONUTF8 = "1" $env:PYTHONIOENCODING = "utf-8" try { [Console]::OutputEncoding = [System.Text.Encoding]::UTF8 } catch {} $env:AHAN_NO_PROXY = "1" $env:NO_PROXY = "localhost,127.0.0.1,::1,$($uri.Host)" if ($InsecureTls) { $env:AHAN_INSECURE_TLS = "1" } if (-not $WorkDir) { $WorkDir = Join-Path $PWD ".ahan-seeker" } New-Item -ItemType Directory -Force -Path $WorkDir | Out-Null $wingetInstallCmd = "winget install --exact --id Python.Python.3.12 --accept-package-agreements --accept-source-agreements" $mcpCompatUrl = "$Server/mcp?toolset=buyer&name_style=underscore" function Test-RealPythonPath { param([string]$CandidatePath) if (-not $CandidatePath) { return $false } if ($CandidatePath -match "WindowsApps") { return $false } return (Test-Path -LiteralPath $CandidatePath -PathType Leaf) } function Find-RealPython { param([string]$LocalWorkDir) # (a) PATH candidates (explicitly skipping Microsoft Store WindowsApps stubs) foreach ($cand in @("python.exe", "py.exe", "python3.exe")) { $cmd = Get-Command $cand -ErrorAction SilentlyContinue if ($cmd -and $cmd.Source -and ($cmd.Source -notmatch "WindowsApps")) { if ($cand -eq "py.exe") { return @{ Exe = $cmd.Source; Prefix = @("-3") } } return @{ Exe = $cmd.Source; Prefix = @() } } } # (b) Standard per-user Python 3.x installs under %LOCALAPPDATA%\Programs\Python\Python3* if ($env:LOCALAPPDATA) { $pyRoot = Join-Path $env:LOCALAPPDATA "Programs\Python" if (Test-Path -LiteralPath $pyRoot -PathType Container) { $dirs = Get-ChildItem -LiteralPath $pyRoot -Directory -Filter "Python3*" -ErrorAction SilentlyContinue | Sort-Object Name -Descending foreach ($d in $dirs) { $p = Join-Path $d.FullName "python.exe" if (Test-RealPythonPath $p) { return @{ Exe = $p; Prefix = @() } } } } } # (c) Portable / bootstrapped Python locations $portableCandidates = @() if ($LocalWorkDir) { $portableCandidates += (Join-Path $LocalWorkDir "py\python.exe") } if ($env:TEMP) { $portableCandidates += (Join-Path $env:TEMP "ahan_py312\python.exe") } foreach ($p in $portableCandidates) { if (Test-RealPythonPath $p) { return @{ Exe = $p; Prefix = @() } } } # (d) uv managed Python (if uv is available) $uvCmd = Get-Command "uv.exe" -ErrorAction SilentlyContinue if (-not $uvCmd) { $uvCmd = Get-Command "uv" -ErrorAction SilentlyContinue } if ($uvCmd -and $uvCmd.Source) { foreach ($ver in @("3.12", "3.11", "3.10")) { try { $found = (& $uvCmd.Source python find $ver 2>$null | Select-Object -First 1) if ($found) { $trimmed = $found.Trim() if (Test-RealPythonPath $trimmed) { return @{ Exe = $trimmed; Prefix = @() } } } } catch {} } } return $null } function Get-RedactedWinInetProxyEnabled { try { $regPath = "HKCU:\Software\Microsoft\Windows\CurrentVersion\Internet Settings" $props = Get-ItemProperty -Path $regPath -ErrorAction SilentlyContinue if ($props -and $props.ProxyEnable -eq 1) { return $true } } catch {} return $false } function Invoke-CurlProbe { param([string]$TargetUrl, [switch]$UseInsecureTls) $curlCmd = Get-Command "curl.exe" -ErrorAction SilentlyContinue if (-not $curlCmd) { return @{ reachable = $false; http_status = 0; error = "curl_missing" } } $argsList = @("-sS", "--noproxy", "*", "--connect-timeout", "5", "--max-time", "10", "-o", "NUL", "-w", "%{http_code}") if ($UseInsecureTls) { $argsList += "-k" } $argsList += $TargetUrl try { $out = & $curlCmd.Source @argsList 2>$null $code = 0 if ($out -and ([int]::TryParse($out.Trim(), [ref]$code))) { return @{ reachable = ($code -ge 200 -and $code -lt 500); http_status = $code } } return @{ reachable = $false; http_status = 0 } } catch { return @{ reachable = $false; http_status = 0 } } } function Invoke-PurePowerShellAction { param( [string]$ActionName, [string]$ServerUrl, [string]$ToolName, [string[]]$ExtraProbeBins, [switch]$UseInsecureTls ) $normAction = $ActionName.Trim().ToLowerInvariant() $arch = if ([Environment]::Is64BitOperatingSystem) { "x64" } else { "x86" } $psVer = $PSVersionTable.PSVersion.ToString() $wingetPresent = [bool](Get-Command "winget.exe" -ErrorAction SilentlyContinue) $proxyEnvPresent = [bool]($env:HTTP_PROXY -or $env:HTTPS_PROXY -or $env:ALL_PROXY -or $env:http_proxy -or $env:https_proxy) $winInetProxy = Get-RedactedWinInetProxyEnabled $probeNames = @("git", "node", "winget", "curl.exe") foreach ($b in $ExtraProbeBins) { if ($b -and ($probeNames -notcontains $b)) { $probeNames += $b } } $probes = @{} foreach ($bn in $probeNames) { $c = Get-Command $bn -ErrorAction SilentlyContinue $present = [bool]($c -and $c.Source -and ($c.Source -notmatch "WindowsApps")) $probes[$bn] = @{ present = $present } } $healthProbe = Invoke-CurlProbe -TargetUrl "$ServerUrl/healthz" -UseInsecureTls:$UseInsecureTls if ($normAction -eq "scan") { $payload = [ordered]@{ ok = $true runtime = "powershell-pure" action = "scan" server = $ServerUrl platform = [ordered]@{ os = "windows" arch = $arch ps_version = $psVer } python = [ordered]@{ found = $false windowsapps_stub_ignored = $true winget_available = $wingetPresent install_cmd = $wingetInstallCmd } network = [ordered]@{ proxy_env_present = $proxyEnvPresent wininet_proxy_enabled = $winInetProxy server_reachable = [bool]$healthProbe.reachable server_http_status = [int]$healthProbe.http_status } probes = $probes mcp_url_compat = $mcpCompatUrl skill_url = "$ServerUrl/skill.md" next_steps = @( "For MCP-capable hosts (Trae / Doubao / strict gateways), connect directly to $mcpCompatUrl (zero Python needed).", "For CLI worker flows (onboard / request-install / apply-setup-kit), install Python 3.12 via: $wingetInstallCmd", "Or re-run seeker.ps1 with -AutoBootstrapPython to fetch a SHA-256-verified embeddable Python into .ahan-seeker\py." ) } Write-Output ($payload | ConvertTo-Json -Depth 6) return } if ($normAction -eq "guide") { $skillPreview = "" $curlCmd = Get-Command "curl.exe" -ErrorAction SilentlyContinue if ($curlCmd) { $cArgs = @("-fsSL", "--noproxy", "*", "--connect-timeout", "5", "--max-time", "10") if ($UseInsecureTls) { $cArgs += "-k" } try { $rawSkill = (& $curlCmd.Source @cArgs "$ServerUrl/skill.md" 2>$null | Out-String) if ($rawSkill) { $skillPreview = ($rawSkill.Substring(0, [Math]::Min($rawSkill.Length, 1200))).Trim() } } catch {} } $payload = [ordered]@{ ok = $true runtime = "powershell-pure" action = "guide" server = $ServerUrl skill_url = "$ServerUrl/skill.md" mcp_url = "$ServerUrl/mcp" mcp_url_compat = $mcpCompatUrl python_found = $false install_python_cmd = $wingetInstallCmd server_reachable = [bool]$healthProbe.reachable skill_excerpt = $skillPreview } Write-Output ($payload | ConvertTo-Json -Depth 6) return } if ($normAction -eq "search") { $packs = @() $curlCmd = Get-Command "curl.exe" -ErrorAction SilentlyContinue if ($curlCmd) { $cArgs = @("-fsSL", "--noproxy", "*", "--connect-timeout", "5", "--max-time", "10") if ($UseInsecureTls) { $cArgs += "-k" } try { $rawPacks = (& $curlCmd.Source @cArgs "$ServerUrl/api/public/packs" 2>$null | Out-String) if ($rawPacks) { $parsed = $rawPacks | ConvertFrom-Json if ($parsed -and $parsed.packs) { $packs = @($parsed.packs) } } } catch {} } $payload = [ordered]@{ ok = $true runtime = "powershell-pure" action = "search" server = $ServerUrl query = $ToolName python_found = $false server_reachable = [bool]$healthProbe.reachable public_pack_count = $packs.Count mcp_url_compat = $mcpCompatUrl recommended_mcp_tool = "ahan_v2_biz_search" next_action = "Connect via MCP ($mcpCompatUrl) or install Python 3.12 ($wingetInstallCmd) for authenticated search and request-install." } Write-Output ($payload | ConvertTo-Json -Depth 6) return } } function Invoke-BootstrapEmbeddablePython { param( [string]$LocalWorkDir, [string]$ZipUrl, [string]$ExpectedSha256, [switch]$UseInsecureTls ) $pyDir = Join-Path $LocalWorkDir "py" $pyBin = Join-Path $pyDir "python.exe" if (Test-RealPythonPath $pyBin) { return @{ ok = $true; exe = $pyBin } } $curlCmd = Get-Command "curl.exe" -ErrorAction SilentlyContinue if (-not $curlCmd) { return @{ ok = $false; error = "curl_missing"; message = "curl.exe is required for -AutoBootstrapPython" } } $zipPath = Join-Path $LocalWorkDir "python-embed.zip" $cArgs = @("-fsSL", "--noproxy", "*", "--connect-timeout", "10", "--max-time", "120") if ($UseInsecureTls) { $cArgs += "-k" } & $curlCmd.Source @cArgs $ZipUrl -o $zipPath if ($LASTEXITCODE -ne 0 -or -not (Test-Path -LiteralPath $zipPath)) { return @{ ok = $false; error = "download_failed"; message = "Failed to download embeddable Python zip (curl exit $LASTEXITCODE)" } } $actualHash = (Get-FileHash -LiteralPath $zipPath -Algorithm SHA256).Hash.ToLowerInvariant() $wantHash = $ExpectedSha256.Trim().ToLowerInvariant() if (-not $wantHash -or $actualHash -ne $wantHash) { Remove-Item -LiteralPath $zipPath -Force -ErrorAction SilentlyContinue return @{ ok = $false; error = "sha256_mismatch"; message = "Embeddable Python SHA-256 mismatch (expected $wantHash, got $actualHash)" } } New-Item -ItemType Directory -Force -Path $pyDir | Out-Null Expand-Archive -LiteralPath $zipPath -DestinationPath $pyDir -Force Remove-Item -LiteralPath $zipPath -Force -ErrorAction SilentlyContinue if (-not (Test-RealPythonPath $pyBin)) { return @{ ok = $false; error = "extract_failed"; message = "python.exe not found after extracting embeddable zip" } } return @{ ok = $true; exe = $pyBin } } # 2. Locate real Python 3.10+ interpreter (explicitly skipping Microsoft Store WindowsApps stubs) $normAction = $Action.Trim().ToLowerInvariant() $purePsActions = @("scan", "guide", "search") $pyInfo = Find-RealPython -LocalWorkDir $WorkDir if (-not $pyInfo) { if ($purePsActions -contains $normAction) { Invoke-PurePowerShellAction -ActionName $normAction -ServerUrl $Server -ToolName $ToolTarget -ExtraProbeBins $ProbeBin -UseInsecureTls:$InsecureTls exit 0 } if ($AutoBootstrapPython) { $boot = Invoke-BootstrapEmbeddablePython -LocalWorkDir $WorkDir -ZipUrl $BootstrapPythonUrl -ExpectedSha256 $BootstrapPythonSha256 -UseInsecureTls:$InsecureTls if ($boot.ok) { $pyInfo = @{ Exe = $boot.exe; Prefix = @() } } else { $errPayload = [ordered]@{ ok = $false python_missing = $true action = $Action runtime = "powershell-pure" error = $boot.error message = $boot.message install_cmd = $wingetInstallCmd mcp_url_compat = $mcpCompatUrl } Write-Output ($errPayload | ConvertTo-Json -Depth 4) exit 2 } } else { $guidePayload = [ordered]@{ ok = $false python_missing = $true action = $Action runtime = "powershell-pure" message = "No real Python 3.10+ interpreter found on PATH or standard Windows locations (WindowsApps Store stub ignored)." install_cmd = $wingetInstallCmd bootstrap_hint = "Re-run with -AutoBootstrapPython to download a SHA-256-verified embeddable Python zip into $WorkDir\py." pure_ps_actions = $purePsActions mcp_url_compat = $mcpCompatUrl } Write-Output ($guidePayload | ConvertTo-Json -Depth 4) exit 2 } } $pyExe = $pyInfo.Exe $pyPrefixArgs = $pyInfo.Prefix # 3. Self-contained 3-file fetch via curl.exe -fsSL --noproxy "*" (default verified HTTPS; opt-in -InsecureTls) $seekerPath = Join-Path $WorkDir "seeker.py" $connectPath = Join-Path $WorkDir "connect.py" $workerPath = Join-Path $WorkDir "worker.py" $downloads = @( @{ Name = "seeker.py"; Url = "$Server/seeker.py"; Dest = $seekerPath }, @{ Name = "connect.py"; Url = "$Server/connect.py"; Dest = $connectPath }, @{ Name = "worker.py"; Url = "$Server/worker.py"; Dest = $workerPath } ) $curlArgs = @("-fsSL", "--noproxy", "*") if ($InsecureTls) { $curlArgs += "-k" } foreach ($item in $downloads) { if ($SkipDownload -and (Test-Path -LiteralPath $item.Dest)) { continue } & curl.exe @curlArgs $item.Url -o $item.Dest if ($LASTEXITCODE -ne 0) { throw "Failed to download $($item.Name) from $($item.Url) (curl.exe exit $LASTEXITCODE)" } } # 4. Invoke seeker.py with dirty-host shielded arguments $pyArgs = @( $seekerPath, "--server", $Server, "--scripts-dir", $WorkDir, "--action", $Action, "--tool-target", $ToolTarget ) if ($Profile) { $pyArgs += @("--profile", $Profile) } if ($CredentialFile) { $pyArgs += @("--credential-file", $CredentialFile) } if ($HomeDir) { $pyArgs += @("--home-dir", $HomeDir) } if ($WorkId) { $pyArgs += @("--work-id", $WorkId) } if ($OutDir) { $pyArgs += @("--out-dir", $OutDir) } if ($IdempotencyKey) { $pyArgs += @("--idempotency-key", $IdempotencyKey) } foreach ($binName in $ProbeBin) { if ($binName) { $pyArgs += @("--probe-bin", $binName) } } if ($InsecureTls) { $pyArgs += "--insecure-tls" } & $pyExe @pyPrefixArgs @pyArgs exit $LASTEXITCODE