Skip to content

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

fix(settings): preserve current proxy drafts on mode switch

vastsa80125039990b
diff --git a/apps/desktop/src/components/settings/NetworkProxySection.tsx b/apps/desktop/src/components/settings/NetworkProxySection.tsx
index 56cec5f87..c35532c46 100644
--- a/apps/desktop/src/components/settings/NetworkProxySection.tsx
+++ b/apps/desktop/src/components/settings/NetworkProxySection.tsx
@@ -11,6 +11,7 @@ import {
DEFAULT_NETWORK_PROXY_BYPASS,
isRelaxedNetworkPolicy,
parseProxyUrl,
+ settingsForInactiveProxyMode,
validateNetworkProxy,
} from "@pi-desktop/shared";
import { api } from "../../lib/api";
@@ -68,10 +69,11 @@ export function NetworkProxySection({
if (mode === saved.mode) return;
setTestState("idle");
if (mode !== "custom") {
- // Carry the custom URL and bypass across the switch: the mode is what
- // changes, and picking Custom again must restore the user's own proxy
- // instead of the built-in default (issue #1541).
- void persist({ mode, url: saved.url, bypass: saved.bypass });
+ // Blur can start an async save, so read the current drafts here instead
+ // of relying on `saved` to have refreshed before this click completes.
+ void persist(
+ settingsForInactiveProxyMode(mode, saved, urlDraft, bypassDraft),
+ );
return;
}
const parsed = parseProxyUrl(urlDraft);
diff --git a/apps/desktop/test/settings-general.test.mjs b/apps/desktop/test/settings-general.test.mjs
index 69c14fe4f..41676560d 100644
--- a/apps/desktop/test/settings-general.test.mjs
+++ b/apps/desktop/test/settings-general.test.mjs
@@ -203,11 +203,11 @@ test("language persists as part of shared app settings", () => {
test("General Network card persists a custom HTTP or SOCKS5 proxy and the relaxed network mode", () => {
assert.match(settingsPageSource, /<NetworkProxySection /);
assert.match(networkProxySource, /settings\.networkRelaxedMode/);
- // A mode switch carries the custom URL and bypass, so choosing Custom again
- // restores the user's own proxy instead of the built-in default (#1541).
+ // A mode switch saves the current drafts, even when the input blur save has
+ // not refreshed `saved` yet, so Custom restores the user's latest values.
assert.match(
networkProxySource,
- /persist\(\{ mode, url: saved\.url, bypass: saved\.bypass \}\)/,
+ /settingsForInactiveProxyMode\(mode, saved, urlDraft, bypassDraft\)/,
);
// Fake-IP tolerance belongs to the network mode now, not to the proxy payload.
assert.doesNotMatch(networkProxySource, /allowFakeIp/);
diff --git a/docs/spec/06-delivery/04-e2e-test-plan.md b/docs/spec/06-delivery/04-e2e-test-plan.md
index 8abf83332..dfef17caa 100644
--- a/docs/spec/06-delivery/04-e2e-test-plan.md
+++ b/docs/spec/06-delivery/04-e2e-test-plan.md
@@ -13317,9 +13317,10 @@ are withdrawn with ADR 0165.
localhost / 127.0.0.1 / ::1 / `<local>`, and Test. Enter
`not-a-proxy` and blur. Confirm an inline invalid-URL error and that
settings are not saved.
- 3. Enter `socks5://127.0.0.1:1080` or `http://127.0.0.1:7890` and blur.
- Confirm `AppSettings.networkProxy.mode` is `custom` after
- `settings.get`.
+ 3. Enter `socks5://127.0.0.1:1080` or `http://127.0.0.1:7890`, then select
+ System immediately without clicking elsewhere. Confirm `settings.get`
+ retains the URL and Bypass while `mode` is `system`. Select Custom again
+ and confirm both values return to the fields and `mode` is `custom`.
4. Click Test against a listening proxy. Confirm a Connected status. Click
Test against a closed port. Confirm a failure status without changing
the saved URL. Enter `http://user:[email protected]:<auth-port>` and
diff --git a/docs/zh-CN/spec/06-delivery/04-e2e-test-plan.md b/docs/zh-CN/spec/06-delivery/04-e2e-test-plan.md
index e9fb52ce6..8d79c57d4 100644
--- a/docs/zh-CN/spec/06-delivery/04-e2e-test-plan.md
+++ b/docs/zh-CN/spec/06-delivery/04-e2e-test-plan.md
@@ -7429,7 +7429,7 @@ eleven-tool-round desktop paths are verified by
- **步骤**:
1. 打开设置 → 常规。确认网络卡提供系统 / 直连 / 自定义。从未配置过代理的配置文件默认是系统。
2. 选择自定义。确认代理 URL、默认绕过列表和测试。输入 `not-a-proxy` 并失焦。确认内联错误且未保存。
- 3. 输入 `socks5://127.0.0.1:1080` 或 `http://127.0.0.1:7890` 并失焦。确认 `settings.get` 中 `networkProxy.mode` 为 `custom`。
+ 3. 输入 `socks5://127.0.0.1:1080` 或 `http://127.0.0.1:7890` 后,立即选择“系统”,中间不要点击其他位置。确认 `settings.get` 中 `mode` 为 `system`,且 URL 与绕过列表仍保留。再选回“自定义”,确认两个值恢复到输入框,且 `mode` 为 `custom`。
4. 对正在监听的代理点测试,确认已连接;对关闭的端口点测试,确认失败且不改已保存 URL。对需要认证的 HTTP / SOCKS5 代理填入 `user:pass@` 后再测,确认是已连接而不是 `net::ERR_NO_SUPPORTED_PROXIES`(issue #490)。
5. 保存自定义代理后,通过已配置供应商发送一条简短提示。确认供应商请求和响应经过代理;即使 SOCKS5 代理将完整 bind 响应合并在一个 TCP 数据块中,请求仍能完成。再确认扩展市场刷新和 models.dev 刷新走代理;绕过列表中的回环地址不走代理。
6. 切到直连再切回系统。确认无需重启应用即可生效。再选回自定义,确认第 3 步的 URL 与绕过列表原样回来(系统/直连期间它们保留在 `AppSettings.networkProxy` 里)。
diff --git a/packages/shared/src/network-proxy.test.ts b/packages/shared/src/network-proxy.test.ts
index 31b15b028..80652fb6f 100644
--- a/packages/shared/src/network-proxy.test.ts
+++ b/packages/shared/src/network-proxy.test.ts
@@ -10,6 +10,7 @@ import {
proxyHasCredentials,
redactProxyUrl,
restoreProxyEnv,
+ settingsForInactiveProxyMode,
snapshotProxyEnv,
stripProxyEnv,
validateNetworkProxy,
@@ -158,6 +159,55 @@ describe("validateNetworkProxy", () => {
},
});
});
+
+ it("uses the current drafts when switching before blur persistence finishes", () => {
+ expect(
+ settingsForInactiveProxyMode(
+ "system",
+ {
+ mode: "custom",
+ url: "socks5://127.0.0.1:1080",
+ bypass: "localhost",
+ },
+ "socks5://127.0.0.1:7891",
+ "localhost,10.8.0.0/24",
+ ),
+ ).toEqual({
+ mode: "system",
+ url: "socks5://127.0.0.1:7891",
+ bypass: "localhost,10.8.0.0/24",
+ });
+ });
+
+ it("retains the last valid URL when the draft is invalid and uses the default for a cleared bypass", () => {
+ expect(
+ settingsForInactiveProxyMode(
+ "direct",
+ {
+ mode: "custom",
+ url: "http://127.0.0.1:7890",
+ bypass: "localhost,10.8.0.0/24",
+ },
+ "not-a-proxy",
+ " ",
+ ),
+ ).toEqual({
+ mode: "direct",
+ url: "http://127.0.0.1:7890",
+ bypass: DEFAULT_NETWORK_PROXY_BYPASS,
+ });
+ });
+
+ it("does not add default proxy values to a profile without a saved proxy", () => {
+ expect(
+ settingsForInactiveProxyMode(
+ "direct",
+ { mode: "system" },
+ "",
+ DEFAULT_NETWORK_PROXY_BYPASS,
+ ),
+ ).toEqual({ mode: "direct" });
+ });
});
describe("chromium and env projections", () => {
diff --git a/packages/shared/src/network-proxy.ts b/packages/shared/src/network-proxy.ts
index db8489a2a..e5d1b0c09 100644
--- a/packages/shared/src/network-proxy.ts
+++ b/packages/shared/src/network-proxy.ts
@@ -229,6 +229,31 @@ export function validateNetworkProxy(
return { ok: true, value: next };
}
+/**
+ * Build settings when leaving Custom. The input blur handler may still be
+ * saving when the mode control is clicked, so use the current drafts and keep
+ * the last valid URL if the draft is invalid.
+ */
+export function settingsForInactiveProxyMode(
+ mode: Exclude<NetworkProxyMode, "custom">,
+ saved: NetworkProxySettings,
+ urlDraft: string,
+ bypassDraft: string,
+): NetworkProxySettings {
+ const parsedUrl = parseProxyUrl(urlDraft);
+ const url = parsedUrl.ok ? parsedUrl.value.href : saved.url;
+ const effectiveBypass = bypassDraft.trim() || DEFAULT_NETWORK_PROXY_BYPASS;
+ const savedEffectiveBypass = saved.bypass ?? DEFAULT_NETWORK_PROXY_BYPASS;
+ const bypass =
+ effectiveBypass === savedEffectiveBypass ? saved.bypass : effectiveBypass;
+
+ return {
+ mode,
+ ...(url ? { url } : {}),
+ ...(bypass ? { bypass } : {}),
+ };
+}
+
export type ChromiumProxyConfig =
| { mode: "system" }
| { mode: "direct" }