sync: WIN11-BOWEI 2026-08-10 MCP审计脚本python回退到conda(MS Store存根修复)

sync_mcp_registrations.ps1 默认用 PATH 上的 python 解析 Codex TOML,
但 Windows 上常见的是 Microsoft Store python 存根(exit 49, 无 stdlib)。
新增 Resolve-PythonCommand: python 不带 tomllib 时自动回退到 skills_env.md
的 CONDA_LANGGRAPH_PYTHON, 再退到 python3/py。显式 -PythonCommand 仍优先。

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
Myfro9
2026-09-02 14:53:28 -05:00
co-authored by Claude Opus 4.8
parent 42edf13936
commit 9c1012f5b8
@@ -18,6 +18,41 @@ $script:CodexBackupMade = $false
$script:Failures = @() $script:Failures = @()
$CodexOnlyEnv = @("PYTHONIOENCODING", "PYTHONUTF8") $CodexOnlyEnv = @("PYTHONIOENCODING", "PYTHONUTF8")
function Test-TomllibPython([string]$Candidate) {
# tomllib needs CPython 3.11+; the Microsoft Store 'python' stub exits 49 and has no stdlib.
if (-not $Candidate) { return $false }
try { & $Candidate -c "import tomllib" 2>&1 | Out-Null } catch { return $false }
return ($LASTEXITCODE -eq 0)
}
function Resolve-PythonCommand([string]$Requested, [bool]$Explicit) {
# An explicit -PythonCommand is authoritative; only auto-probe the default.
if ($Explicit) { return $Requested }
if (Test-TomllibPython $Requested) { return $Requested }
$envFile = "$env:USERPROFILE\skills_env.md"
if (Test-Path -LiteralPath $envFile) {
$line = Select-String -LiteralPath $envFile -Pattern '^\s*CONDA_LANGGRAPH_PYTHON\s*=\s*(.+?)\s*$' |
Select-Object -First 1
if ($line) {
$cand = $line.Matches[0].Groups[1].Value.Trim()
if ((Test-Path -LiteralPath $cand) -and (Test-TomllibPython $cand)) {
Write-Host "python: falling back to CONDA_LANGGRAPH_PYTHON ($cand)"
return $cand
}
}
}
# Single-token candidates only: '& $cmd' treats the whole string as one exe name.
foreach ($cand in @("python3", "py")) {
if ((Get-Command $cand -ErrorAction SilentlyContinue) -and (Test-TomllibPython $cand)) {
Write-Host "python: falling back to '$cand'"
return $cand
}
}
throw "No tomllib-capable Python found ('$Requested' unusable). Pass -PythonCommand <path to CPython 3.11+>."
}
$PythonCommand = Resolve-PythonCommand $PythonCommand $PSBoundParameters.ContainsKey('PythonCommand')
function ConvertTo-StringMap($Object) { function ConvertTo-StringMap($Object) {
$result = [ordered]@{} $result = [ordered]@{}
if ($null -eq $Object) { return $result } if ($null -eq $Object) { return $result }