Skip to content

Local-first AI coding agent desktop: Electron + Rust host core + pi Agent Harness + user-installable plugins

fix(oauth): send pi User-Agent on Anthropic token requests

rekazer0351d242e8d5d
diff --git a/apps/desktop/test/anthropic-oauth-retry.test.mjs b/apps/desktop/test/anthropic-oauth-retry.test.mjs
index c6eaab700..08dad0af6 100644
--- a/apps/desktop/test/anthropic-oauth-retry.test.mjs
+++ b/apps/desktop/test/anthropic-oauth-retry.test.mjs
@@ -136,6 +136,18 @@ test("real refresh retries inside the provider lock and preserves request header
});
});
+test("token requests identify the client instead of inheriting a browser User-Agent", async () => {
+ // Electron's main-process fetch is net.fetch, which defaults to Chromium's
+ // "Mozilla/5.0 ..." User-Agent; the token endpoint answers that with a
+ // permanent 429 (issue #1337). Login and refresh must both send pi's own UA.
+ await fixture([{}], async ({ requests, signIn, oauth, seed }) => {
+ assert.equal((await signIn()).kind, "done");
+ await oauth.resolveAuth(seed());
+ assert.deepEqual(requests.map(req => req.body.grant_type), ["authorization_code", "refresh_token"]);
+ for (const req of requests) assert.match(req.headers["user-agent"], /^pi \(/);
+ });
+});
+
test("429 exhaustion is three attempts with safe recovery text and no stored partial login", async () => {
await fixture([limited()], async ({ requests, host, signIn }) => {
const result = await signIn();
diff --git a/docs/spec/03-runtime/11-provider-model-system.md b/docs/spec/03-runtime/11-provider-model-system.md
index 188a6ed96..05233b501 100644
--- a/docs/spec/03-runtime/11-provider-model-system.md
+++ b/docs/spec/03-runtime/11-provider-model-system.md
@@ -476,6 +476,13 @@ same vendor key.
### Anthropic token endpoint rate limits
+Every token request sends pi-ai's own `User-Agent` (`pi (<platform> <release>;
+<arch>)`). Electron Main replaces the global `fetch` with `net.fetch`, whose
+default is Chromium's browser `User-Agent`; the token endpoint answers browser
+and `claude-code/` agents with a 429 `rate_limit_error` on every attempt, so
+without an explicit agent sign-in could never succeed (issue #1337). A custom
+`User-Agent` on the provider row still overrides it.
+
The pinned pi-ai 1.1.0 patch gives Anthropic authorization-code exchange and
refresh a shared, bounded token-request policy: retry only an explicit HTTP
429, at most three total requests. Wait at least 1 s then 2 s, or longer when
diff --git a/docs/zh-CN/spec/03-runtime/11-provider-model-system.md b/docs/zh-CN/spec/03-runtime/11-provider-model-system.md
index 05aba2aa5..8b91191a7 100644
--- a/docs/zh-CN/spec/03-runtime/11-provider-model-system.md
+++ b/docs/zh-CN/spec/03-runtime/11-provider-model-system.md
@@ -387,6 +387,12 @@ Codex CLI 版本(`CODEX_MODELS_CLIENT_VERSION`),账户模型缺失时调
### Anthropic token 端点限流
+每次 token 请求都发送 pi-ai 自己的 `User-Agent`(`pi (<platform> <release>;
+<arch>)`)。Electron Main 把全局 `fetch` 替换为 `net.fetch`,其默认值是
+Chromium 的浏览器 `User-Agent`;token 端点对浏览器和 `claude-code/` 的
+`User-Agent` 每次都返回 429 `rate_limit_error`,不显式设置时登录永远无法
+成功(issue #1337)。服务商行上自定义的 `User-Agent` 仍可覆盖它。
+
固定版本 pi-ai 1.1.0 的仓库补丁为 Anthropic 授权码交换与刷新提供同一套
有限策略:只重试明确的 HTTP 429,最多总共三次请求。先等待至少 1 秒、再
等待至少 2 秒;若 `Retry-After` 给出更长的秒数或 HTTP 日期,则遵守该时间。
diff --git a/patches/@[email protected] b/patches/@[email protected]
index d15666eaf..4ab80d45d 100644
--- a/patches/@[email protected]
+++ b/patches/@[email protected]
@@ -758,10 +758,18 @@ index 4e36f35..c817c2e 100644
}
export function resolveSamplingParams(model, thinkingLevel, requestParams) {
diff --git a/dist/auth/oauth/anthropic.js b/dist/auth/oauth/anthropic.js
-index 0823512..3af4225 100644
+index 0823512..713d38a 100644
--- a/dist/auth/oauth/anthropic.js
+++ b/dist/auth/oauth/anthropic.js
-@@ -66,21 +66,90 @@ function formatErrorDetails(error) {
+@@ -4,6 +4,7 @@
+ * NOTE: This module uses Node.js http.createServer for the OAuth callback server.
+ * It is only intended for CLI use, not browser environments.
+ */
++import { getPiUserAgent } from "../../utils/pi-user-agent.js";
+ import { getProviderEnvValue } from "../../utils/provider-env.js";
+ import { startOAuthCallbackServer, waitForCallbackOrManualInput } from "./callback-server.js";
+ import { generatePKCE } from "./pkce.js";
+@@ -66,21 +67,95 @@ function formatErrorDetails(error) {
}
return String(error);
}
@@ -835,6 +843,11 @@ index 0823512..3af4225 100644
+ headers: {
+ "Content-Type": "application/json",
+ Accept: "application/json",
++ // Always identify the client. Electron's main-process fetch
++ // (net.fetch) otherwise sends Chromium's browser User-Agent,
++ // and the token endpoint answers browser or claude-code/ UAs
++ // with a permanent 429 rate_limit_error (issue #1337).
++ "User-Agent": getPiUserAgent(),
+ },
+ body: serialized,
+ signal: requestSignal,
@@ -865,7 +878,7 @@ index 0823512..3af4225 100644
}
async function exchangeAuthorizationCode(code, state, verifier, redirectUri, signal) {
let responseBody;
-@@ -95,6 +164,8 @@ async function exchangeAuthorizationCode(code, state, verifier, redirectUri, sig
+@@ -95,6 +170,8 @@ async function exchangeAuthorizationCode(code, state, verifier, redirectUri, sig
}, signal);
}
catch (error) {
@@ -874,7 +887,7 @@ index 0823512..3af4225 100644
throw new Error(`Token exchange request failed. url=${TOKEN_URL}; redirect_uri=${redirectUri}; response_type=authorization_code; details=${formatErrorDetails(error)}`);
}
let tokenData;
-@@ -102,7 +173,7 @@ async function exchangeAuthorizationCode(code, state, verifier, redirectUri, sig
+@@ -102,7 +179,7 @@ async function exchangeAuthorizationCode(code, state, verifier, redirectUri, sig
tokenData = JSON.parse(responseBody);
}
catch (error) {
@@ -883,7 +896,7 @@ index 0823512..3af4225 100644
}
return {
type: "oauth",
-@@ -213,6 +284,8 @@ async function refreshAnthropicToken(refreshToken, signal) {
+@@ -213,6 +290,8 @@ async function refreshAnthropicToken(refreshToken, signal) {
}, signal);
}
catch (error) {
@@ -892,7 +905,7 @@ index 0823512..3af4225 100644
throw new Error(`Anthropic token refresh request failed. url=${TOKEN_URL}; details=${formatErrorDetails(error)}`);
}
let data;
-@@ -220,7 +293,7 @@ async function refreshAnthropicToken(refreshToken, signal) {
+@@ -220,7 +299,7 @@ async function refreshAnthropicToken(refreshToken, signal) {
data = JSON.parse(responseBody);
}
catch (error) {
diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml
index 38cc74d8c..4ebdf80bb 100644
--- a/pnpm-lock.yaml
+++ b/pnpm-lock.yaml
@@ -200,7 +200,7 @@ overrides:
patchedDependencies:
'@earendil-works/[email protected]': a2c96619d0f9a1d643a6c6a555ba6a7fb533147336c181f7d3b7f847be33a6eb
- '@earendil-works/[email protected]': 9f3713107628b7e52bc90d1bab011e3d819fec598ce1eac47d389aad0bd96dcd
+ '@earendil-works/[email protected]': 2aa7a30fca930a08ebdff65f82d2fc47aeb1cd941d34aae2e50e13b309ef2705
'@earendil-works/[email protected]': a2878f6aa1d66afa239d6be4252403a73df48b702b3e582aaae04bd90bf3d354
importers:
@@ -228,7 +228,7 @@ importers:
devDependencies:
'@earendil-works/pi-ai':
specifier: 1.1.0
- version: 1.1.0(patch_hash=9f3713107628b7e52bc90d1bab011e3d819fec598ce1eac47d389aad0bd96dcd)(@aws-sdk/[email protected])(@smithy/[email protected])([email protected])([email protected])([email protected])([email protected])
+ version: 1.1.0(patch_hash=2aa7a30fca930a08ebdff65f82d2fc47aeb1cd941d34aae2e50e13b309ef2705)(@aws-sdk/[email protected])(@smithy/[email protected])([email protected])([email protected])([email protected])([email protected])
'@earendil-works/pi-mcp':
specifier: 1.1.0
version: 1.1.0
@@ -423,7 +423,7 @@ importers:
version: 1.1.0(patch_hash=a2c96619d0f9a1d643a6c6a555ba6a7fb533147336c181f7d3b7f847be33a6eb)(@aws-sdk/[email protected])(@smithy/[email protected])([email protected])([email protected])([email protected])([email protected])
'@earendil-works/pi-ai':
specifier: 1.1.0
- version: 1.1.0(patch_hash=9f3713107628b7e52bc90d1bab011e3d819fec598ce1eac47d389aad0bd96dcd)(@aws-sdk/[email protected])(@smithy/[email protected])([email protected])([email protected])([email protected])([email protected])
+ version: 1.1.0(patch_hash=2aa7a30fca930a08ebdff65f82d2fc47aeb1cd941d34aae2e50e13b309ef2705)(@aws-sdk/[email protected])(@smithy/[email protected])([email protected])([email protected])([email protected])([email protected])
'@earendil-works/pi-coding-agent':
specifier: 1.1.0
version: 1.1.0(patch_hash=a2878f6aa1d66afa239d6be4252403a73df48b702b3e582aaae04bd90bf3d354)(@aws-sdk/[email protected])(@smithy/[email protected])([email protected])([email protected])([email protected])
@@ -5327,7 +5327,7 @@ snapshots:
'@earendil-works/[email protected](patch_hash=a2c96619d0f9a1d643a6c6a555ba6a7fb533147336c181f7d3b7f847be33a6eb)(@aws-sdk/[email protected])(@smithy/[email protected])([email protected])([email protected])([email protected])([email protected])':
dependencies:
- '@earendil-works/pi-ai': 1.1.0(patch_hash=9f3713107628b7e52bc90d1bab011e3d819fec598ce1eac47d389aad0bd96dcd)(@aws-sdk/[email protected])(@smithy/[email protected])([email protected])([email protected])([email protected])([email protected])
+ '@earendil-works/pi-ai': 1.1.0(patch_hash=2aa7a30fca930a08ebdff65f82d2fc47aeb1cd941d34aae2e50e13b309ef2705)(@aws-sdk/[email protected])(@smithy/[email protected])([email protected])([email protected])([email protected])([email protected])
typebox: 1.3.27
transitivePeerDependencies:
- '@aws-sdk/credential-provider-node'
@@ -5342,7 +5342,7 @@ snapshots:
- ws
- zod
- '@earendil-works/[email protected](patch_hash=9f3713107628b7e52bc90d1bab011e3d819fec598ce1eac47d389aad0bd96dcd)(@aws-sdk/[email protected])(@smithy/[email protected])([email protected])([email protected])([email protected])([email protected])':
+ '@earendil-works/[email protected](patch_hash=2aa7a30fca930a08ebdff65f82d2fc47aeb1cd941d34aae2e50e13b309ef2705)(@aws-sdk/[email protected])(@smithy/[email protected])([email protected])([email protected])([email protected])([email protected])':
dependencies:
'@anthropic-ai/sdk': 0.129.0([email protected])
'@aws-sdk/client-bedrock-runtime': 3.1127.0
@@ -5367,7 +5367,7 @@ snapshots:
- ws
- zod
- '@earendil-works/[email protected](patch_hash=9f3713107628b7e52bc90d1bab011e3d819fec598ce1eac47d389aad0bd96dcd)(@aws-sdk/[email protected])(@smithy/[email protected])([email protected])([email protected])([email protected])([email protected])':
+ '@earendil-works/[email protected](patch_hash=2aa7a30fca930a08ebdff65f82d2fc47aeb1cd941d34aae2e50e13b309ef2705)(@aws-sdk/[email protected])(@smithy/[email protected])([email protected])([email protected])([email protected])([email protected])':
dependencies:
'@anthropic-ai/sdk': 0.129.0([email protected])
'@aws-sdk/client-bedrock-runtime': 3.1127.0
@@ -5400,7 +5400,7 @@ snapshots:
dependencies:
'@earendil-works/chord': 1.1.0
'@earendil-works/pi-agent-core': 1.1.0(patch_hash=a2c96619d0f9a1d643a6c6a555ba6a7fb533147336c181f7d3b7f847be33a6eb)(@aws-sdk/[email protected])(@smithy/[email protected])([email protected])([email protected])([email protected])([email protected])
- '@earendil-works/pi-ai': 1.1.0(patch_hash=9f3713107628b7e52bc90d1bab011e3d819fec598ce1eac47d389aad0bd96dcd)(@aws-sdk/[email protected])(@smithy/[email protected])([email protected])([email protected])([email protected])([email protected])
+ '@earendil-works/pi-ai': 1.1.0(patch_hash=2aa7a30fca930a08ebdff65f82d2fc47aeb1cd941d34aae2e50e13b309ef2705)(@aws-sdk/[email protected])(@smithy/[email protected])([email protected])([email protected])([email protected])([email protected])
'@earendil-works/pi-codemode': 1.1.0
'@earendil-works/pi-mcp': 1.1.0
'@earendil-works/pi-tui': 1.1.0