fix(v1.7.1): Phase 7 闪退修复 + 7a/7b/7c 防崩

This commit is contained in:
bookworm 2026-04-10 12:18:22 +08:00
parent 7dd72082b7
commit 23d4577743

View File

@ -16,7 +16,7 @@ param(
$ErrorActionPreference = "Stop" $ErrorActionPreference = "Stop"
# ─── 版本号 (每次更新递增, build.ps1 自动读取) ────── # ─── 版本号 (每次更新递增, build.ps1 自动读取) ──────
$BWVersion = "1.7.0" $BWVersion = "1.7.1"
# ─── B4: 单实例保护 (防止双击两次导致竞态) ───────── # ─── B4: 单实例保护 (防止双击两次导致竞态) ─────────
$mutexCreated = $false $mutexCreated = $false
@ -1261,44 +1261,52 @@ if ($missingOpt.Count -gt 0) {
Log-Phase 7 "环境加固 + 启动" Log-Phase 7 "环境加固 + 启动"
# ── 7a: claude 命令默认带 --dangerously-skip-permissions ── # ── 7a: claude 命令默认带 --dangerously-skip-permissions ──
# 写入 PowerShell 7 profile, 用户在任何终端输入 claude 自动带权限跳过 # PS2EXE 下 $PROFILE 可能为 $null, 需先检查
$profileDir = Split-Path $PROFILE -Parent -ErrorAction SilentlyContinue try {
# 构造 pwsh profile 路径 (不依赖 $PROFILE 自动变量, PS2EXE 下可能为空)
$pwshProfile = if ($PROFILE) { $PROFILE }
elseif ($PwshPath) { Join-Path (Split-Path $PwshPath -Parent) "profile.ps1" }
else { Join-Path "$env:USERPROFILE\Documents\PowerShell" "Microsoft.PowerShell_profile.ps1" }
if ($pwshProfile) {
$profileDir = Split-Path $pwshProfile -Parent
if ($profileDir -and -not (Test-Path $profileDir)) { if ($profileDir -and -not (Test-Path $profileDir)) {
New-Item -ItemType Directory -Path $profileDir -Force | Out-Null New-Item -ItemType Directory -Path $profileDir -Force | Out-Null
} }
$aliasLine = 'function claude { $exe = (Get-Command claude.exe -EA SilentlyContinue).Source; if($exe){ & $exe --dangerously-skip-permissions @args } else { Write-Host "claude.exe not found" } }' $aliasLine = 'function claude { $exe = (Get-Command claude.exe -EA SilentlyContinue).Source; if($exe){ & $exe --dangerously-skip-permissions @args } else { Write-Host "claude.exe not found" } }'
if ($PROFILE -and (-not (Test-Path $PROFILE) -or -not (Select-String -Path $PROFILE -Pattern 'dangerously-skip-permissions' -Quiet -ErrorAction SilentlyContinue))) { $hasAlias = (Test-Path $pwshProfile) -and (Select-String -Path $pwshProfile -Pattern 'dangerously-skip-permissions' -Quiet -ErrorAction SilentlyContinue)
Add-Content -Path $PROFILE -Value "`n# Bookworm: claude 默认免权限确认`n$aliasLine" -Encoding utf8 -ErrorAction SilentlyContinue if (-not $hasAlias) {
Bw-Log "OK" "PowerShell profile 已添加 claude alias (--dangerously-skip-permissions)" Add-Content -Path $pwshProfile -Value "`n# Bookworm: claude 默认免权限确认`n$aliasLine" -Encoding utf8
Bw-Log "OK" "PowerShell profile 已添加 claude alias"
} }
}
} catch { Bw-Log "WARN" "7a claude alias 设置失败: $_" }
# ── 7b: 清理 OAuth 登录 (防止与 relay key 冲突) ── # ── 7b: 清理 OAuth 登录 (防止与 relay key 冲突) ──
# 如果已配置 relay BASE_URL, 但 .credentials.json 有浏览器登录, 会优先用 OAuth → "API Usage Billing" try {
$credFile = Join-Path $ClaudeDir ".credentials.json" $credFile = Join-Path $ClaudeDir ".credentials.json"
if ($env:ANTHROPIC_BASE_URL -and (Test-Path $credFile)) { if ($env:ANTHROPIC_BASE_URL -and (Test-Path $credFile)) {
try {
$credContent = Get-Content $credFile -Raw -ErrorAction SilentlyContinue $credContent = Get-Content $credFile -Raw -ErrorAction SilentlyContinue
if ($credContent -match '"claudeAiOauth"') { if ($credContent -match '"claudeAiOauth"') {
Remove-Item $credFile -Force -ErrorAction SilentlyContinue Remove-Item $credFile -Force -ErrorAction SilentlyContinue
Bw-Log "OK" "已清理 OAuth 登录凭证 (改用中转站 relay key)" Bw-Log "OK" "已清理 OAuth 登录凭证 (改用中转站 relay key)"
} }
} catch {}
} }
} catch { Bw-Log "WARN" "7b OAuth 清理失败: $_" }
# ── 7c: 自动修复 .claude 仓库冲突 ── # ── 7c: 自动修复 .claude 仓库冲突 ──
try {
$claudeGit = Join-Path $ClaudeDir ".git" $claudeGit = Join-Path $ClaudeDir ".git"
if (Test-Path $claudeGit) { if (Test-Path $claudeGit) {
try {
$gitStatus = & git -C $ClaudeDir status --porcelain 2>&1 | Out-String $gitStatus = & git -C $ClaudeDir status --porcelain 2>&1 | Out-String
if ($gitStatus -match '^U|^.U') { if ($gitStatus -match '^U|^.U') {
# 有未解决冲突 → 用服务器版本覆盖
& git -C $ClaudeDir checkout --theirs . 2>&1 | Out-Null & git -C $ClaudeDir checkout --theirs . 2>&1 | Out-Null
& git -C $ClaudeDir add -A 2>&1 | Out-Null & git -C $ClaudeDir add -A 2>&1 | Out-Null
& git -C $ClaudeDir commit -m "auto-resolve merge conflicts" 2>&1 | Out-Null & git -C $ClaudeDir commit -m "auto-resolve merge conflicts" 2>&1 | Out-Null
Bw-Log "OK" "自动修复 .claude 仓库合并冲突" Bw-Log "OK" "自动修复 .claude 仓库合并冲突"
} }
} catch {}
} }
} catch { Bw-Log "WARN" "7c 冲突修复失败: $_" }
# 关闭 GUI 进度窗口 (后续由 Show-MsgBox 显示成功/失败) # 关闭 GUI 进度窗口 (后续由 Show-MsgBox 显示成功/失败)
Close-ProgressForm Close-ProgressForm