Skip to content

fix: claude subprocess auth failure (exit code 1) when using HTTP proxy#230

Open
cccccccAi wants to merge 1 commit intoop7418:mainfrom
cccccccAi:fix/proxy-auth-subprocess
Open

fix: claude subprocess auth failure (exit code 1) when using HTTP proxy#230
cccccccAi wants to merge 1 commit intoop7418:mainfrom
cccccccAi:fix/proxy-auth-subprocess

Conversation

@cccccccAi
Copy link

Problem

When CodePilot is launched from Dock/Finder (not from a terminal), HTTPS_PROXY/HTTP_PROXY are absent from the Electron process environment. As a result, the claude subprocess cannot reach the Anthropic API and exits with code 1.

Symptoms:

  • is_error: true, input_tokens: 0, output_tokens: 0
  • Error message: "Claude Code process exited with an error... Invalid or missing API Key"
  • Works fine when CodePilot is launched from a terminal that has proxy env vars set

Affected users: Anyone using a system-level HTTP proxy to access the internet (common in mainland China and enterprise environments).

Root Cause

The proxy is typically defined in ~/.zshrc / ~/.bashrc. While loadUserShellEnv() in electron/main.ts runs zsh -ilc env to load shell variables, this can fail silently in certain GUI app launch contexts, leaving HTTPS_PROXY out of the subprocess environment.

Additionally, users who authenticate via claude auth login (subscription / OAuth) store their token in the macOS Keychain — not in a file or env var — so CodePilot's SDK subprocess has no credentials to fall back on.

Fix

src/lib/platform.ts — Add getClaudeOAuthTokenFromKeychain():

Reads the OAuth accessToken from the macOS Keychain entry "Claude Code-credentials" (where claude auth login stores credentials). Returns null on non-macOS or if not found.

src/lib/claude-client.ts — Use Keychain token as fallback:

When no active provider and no ANTHROPIC_API_KEY/ANTHROPIC_AUTH_TOKEN is present in the environment, automatically read the OAuth token from Keychain and inject it as ANTHROPIC_AUTH_TOKEN before spawning the claude subprocess.

How to Reproduce

  1. Install Claude Code CLI and authenticate via claude auth login (subscription user)
  2. Set proxy in shell config: export HTTPS_PROXY=http://your-proxy:port in ~/.zshrc
  3. Launch CodePilot from Dock (not from terminal)
  4. Send any message → error: exit code 1

Testing

Verified locally:

  • CodePilot launched without proxy env → previously failed with exit code 1
  • CodePilot launched with proxy env (workaround) → works ✅
  • After this fix, Keychain token is injected correctly and the subprocess authenticates ✅
Claude 连接状态: { "connected": true, "version": "2.1.71 (Claude Code)" }
Claude 响应: 我是 Claude Code,Anthropic 开发的 AI 编程助手...
is_error: false ✅

问题:
CodePilot 从 Dock/Finder 启动时,HTTPS_PROXY/HTTP_PROXY 不在
Electron 进程的 process.env 中,导致 claude 子进程无法出网,
向 Anthropic API 鉴权失败,返回 exit code 1,
表现为 is_error: true / input_tokens: 0。

此问题在使用代理上网的用户(中国大陆等地区)中普遍存在。

修复方案:
1. platform.ts — 新增 getClaudeOAuthTokenFromKeychain()
   从 macOS Keychain 的 "Claude Code-credentials" 条目读取
   OAuth accessToken,作为 ANTHROPIC_AUTH_TOKEN 的兜底来源。
   支持通过 claude auth login(订阅用户)完成鉴权,
   无需在 CodePilot 中单独配置 API Key。

2. claude-client.ts — 无 active provider 且无环境变量 token 时,
   自动从 Keychain 读取 OAuth token 并注入 ANTHROPIC_AUTH_TOKEN,
   确保 claude 子进程获得有效凭证。

复现步骤:
- 安装 Claude Code CLI 并通过 claude auth login 登录(订阅用户)
- 系统依赖代理上网(HTTPS_PROXY 设置在 .zshrc 而非系统偏好设置)
- 从 Dock 启动 CodePilot(非终端启动)
- 在 CodePilot 中发送消息 → 报错 exit code 1

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant