Skip to content

feat(desktop): add proxy settings to preferences - #65540

Open
metif12 wants to merge 5 commits into
anomalyco:devfrom
metif12:desktop-proxy-setting
Open

metif12 wants to merge 5 commits into
anomalyco:devfrom
metif12:desktop-proxy-setting

Conversation

@metif12

@metif12 metif12 commented Nov 22, 2026 •

Copy link
Copy Markdown

Issue for this PR

Closes #27892

Type of change

  • New feature

What does this PR do?

On Windows the desktop app doesn't pick up the system proxy for the agent's HTTP calls, so webfetch fails for sites only reachable through a proxy. This adds a Proxy section under Preferences with an on/off switch, HTTP and HTTPS proxy URLs, a comma-separated bypass list, and an explicit Apply button. Applied settings are saved in the desktop settings store and configured on the Electron session right away; proxy env vars are written non-destructively so service processes started afterwards inherit them. Loopback is always bypassed and existing proxy env vars are restored when the switch is off. It does not stop or restart the shared background service. The UI states plainly that agent requests use the proxy only when the service starts with these settings, and that restarting Desktop does not restart the shared service. A server-read proxy config is the proper fix for that limitation and is awaiting maintainer direction.

How did you verify your code works?

Typecheck (bunx tsc -b) is clean in packages/app and packages/desktop. Eight unit tests cover config parsing, bypass merging, and restoring the user's original proxy env; they pass, and I confirmed they fail when I flip the enable check. I couldn't launch the Electron app here, so the GUI itself is unverified.

Screenshots / recordings

Not included — I couldn't run the desktop app in this environment. I can add before/after screenshots if needed.

Checklist

  • I have tested my changes locally
  • I have not included unrelated changes in this PR

Added by the opencode bot (AI-generated).

Related: #24981, #51247, #26629, #50476, #51022

@github-actions github-actions Bot added the needs:compliance This means the issue will auto-close after 2 hours. label Nov 22, 2026

@opencodejr opencodejr Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I couldn't run this one: it needs the Electron desktop app, and this environment has no Electron binary or display. The 4 new tests pass, and tsc -b is clean in packages/desktop and packages/app. Reading the code, I think two things will break existing users even with the setting off:

  • Startup ordering: applyProxySettings touches session.defaultSession from a fiber forked before app.whenReady().
  • Setting off: the Electron session gets an empty proxy config, which turns off the OS proxy.

In smaller cases, enabling the setting with only one URL clears the user's other proxy variables. Details are inline.

Public API: unchanged. The new desktop IPC RPCs (AppGetProxySettings/AppSetProxySettings), the optional Platform.getProxySettings/setProxySettings methods and the proxySettings settings-store key are internal to the desktop app.

The linked issue mainly asks for the Windows system proxy to be honoured. This PR adds a manual proxy setting, which is useful, but someone who expects the system proxy to just work will still need to enter it by hand.

Since I couldn't drive the desktop app, could you add a screenshot of the new Proxy section? A short check would also help: with the setting off and a system or env proxy configured, Electron's own traffic and the sidecar should still use that proxy.

Related: #24981, #51247, #26629, #50476, #51022


export const prepareApplicationEnvironment = Effect.gen(function* () {
yield* loadSystemCertificates
yield* Effect.sync(applyProxySettings)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

prepareApplicationEnvironment is forked in desktop-initialization.ts before app.whenReady(). applyProxySettings reads session.defaultSession, and Electron throws when that is used before the app is ready. Inside Effect.sync the throw becomes a defect that ends the fiber, so loadProxyEnvironment (the existing env-based proxy plus loopback NO_PROXY) and logging.startNetwork would never run, for every user, setting on or off. Could you keep the process.env part here, before the sidecar starts, and move the session.setProxy call after ready (e.g. next to applySpellcheck() in prepareDesktop)?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in aef4fed: the env part stays in prepareApplicationEnvironment before the sidecar starts, and the Electron session proxy is applied from prepareDesktop after app.whenReady(). Live saves call both through setProxySettings.


const rules = proxyRules(value)

const config: Parameters<typeof session.defaultSession.setProxy>[0] = rules

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With the setting off this calls setProxy({}). Per Electron's ProxyConfig docs, mode defaults to fixed_servers when there is no pacScript, and with no rules that means a direct connection. So every existing user's Electron session would stop following the OS proxy, which contradicts "behaviour is unchanged when the setting is off". Pass { mode: "system" } when disabled, or skip setProxy entirely unless the setting has been enabled during this run.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in aef4fed: when disabled we now call setProxy with mode system, so Electron keeps following the OS proxy instead of forcing a direct connection.

].join(",")
}

export function proxyEnvironment(value: ProxySettings) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When the setting is enabled but one URL is blank, this writes HTTP_PROXY="" / HTTPS_PROXY="" and overwrites a proxy the user already set in their environment. With both URLs blank, enabling the switch actually removes their proxy. NO_PROXY is also replaced rather than merged with any existing env value. Only emit the variables that have a value, and merge the bypass list with the existing NO_PROXY/no_proxy. Related: on disable, applyProxySettings deletes those keys even if the user had them set before launch. A change made while the app runs also doesn't re-run http.setGlobalProxyFromEnv(), so the main process keeps its old Node proxy.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in aef4fed: only URLs with a value are written, NO_PROXY is merged with existing NO_PROXY/no_proxy plus loopback, and the original values including absent keys are restored on disable. Live saves also refresh the Node global proxy. Saving now respawns the service so the agent picks up the change immediately.

@github-actions github-actions Bot removed the needs:compliance This means the issue will auto-close after 2 hours. label Nov 22, 2026
@github-actions

Copy link
Copy Markdown
Contributor

Thanks for updating your PR! It now meets our contributing guidelines. 👍

@metif12

metif12 commented Nov 22, 2026

Copy link
Copy Markdown
Author

Addressed all three inline findings in aef4fed and pushed to this branch. Env application stays before the sidecar starts, the Electron session proxy now runs after app.whenReady() and uses mode system when off, and proxy env changes no longer clobber the user values. Saving a setting respawns the background service in place so the new proxy applies immediately and sessions reconnect through the existing service-change path; no forced refetch was added. Typecheck is clean in app and desktop and 8 unit tests pass. On scope: #27892 explicitly requests a proxy field in the desktop settings UI, so this implements that; automatic OS proxy detection for the agent process is separate work. I could not run Electron in this environment, so no screenshot is included yet.

@opencodejr opencodejr Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I couldn't run this either: it needs the Electron app, and this environment has none. I checked it by reading the code. The proxy tests in packages/desktop/src/main/native/ pass, and tsc -b is clean in packages/desktop and packages/app.

My three earlier comments are resolved:

  • The session proxy is now applied after the app is ready.
  • With the setting off, Electron uses mode: "system".
  • Blank URLs no longer clear the user's proxy variables, NO_PROXY is merged with the existing value, the launch values come back when the setting is turned off, and the Node proxy is refreshed on change.

The new service restart is the blocker. It bypasses BackgroundServiceMachine, so windows are never told the new service address. It also stops the user-level background service that the CLI and TUI share, on every field save. Details are inline.

The shared service also limits the approach. A service that is already running when Desktop starts never gets the proxy variables, and only picks them up once the user changes the setting. A proxy setting the server reads itself, like the opencode.json idea in #24981, would apply wherever the server runs and need no restart. It's worth agreeing on the direction with maintainers before going further.

Public API: unchanged. The new restartService/getProxySettings/setProxySettings methods on the desktop Platform type, the app IPC RPCs and the proxySettings store key are all internal to Desktop.

Since the UI is unverified, before/after screenshots of the Proxy section would help.

Related: #24981, #51247, #26629, #50476, #51022

})

// Proxy settings live in the main process environment, so the service must be respawned to inherit a later change.
const restart = Effect.fn("BackgroundService.restart")(function* () {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This restart runs outside BackgroundServiceMachine, and nothing calls publish with the new credentials. SidecarCredentials isn't updated and no SidecarChanged is emitted, and AppRestartService only maps the result for the caller, which the platform wrapper then discards. If the new service comes up on another port or with new credentials, every window keeps talking to the one that was just stopped. That contradicts "sessions reconnect through the existing service-change handling". It can also race a reconnect the machine is already running. If a restart is kept, it should go through the machine, e.g. as a reconnect that stops first, so that publishing and waiters work as usual.

More importantly, Service.stop stops the shared user-level background service (the service.json in XDG state), which the CLI and TUI also connect to. Changing a desktop proxy setting would kill their sessions.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed. Removed the service restart entirely in d8a43d3, including the new RPC and service-machine changes. Settings now commit once from an explicit Apply button; the Electron session picks them up immediately and service processes started later inherit the env vars. The shared CLI/TUI service is no longer touched. I agree a server-read proxy config is the right direction for agent requests and have asked for maintainer direction on it.

load: async (source) => ({ value: await source.get(), meta: undefined }),
write: async (value, source) => {
await source.set(value)
await platform.restartService?.()

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Every write restarts the service: the switch and each of the three text fields commit on change. Filling in the form stops and respawns the background service up to four times and interrupts any running agent each time. Even with a safe restart, it should run once, when the user explicitly applies the settings, not on every field save.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in d8a43d3: the fields are now local draft state and nothing is written until the explicit Apply button is clicked, so there is one save and no repeated propagation while filling the form.

@metif12

metif12 commented Nov 22, 2026

Copy link
Copy Markdown
Author

Thanks for the second review. Both new findings are addressed in d8a43d3: the service restart and its RPC are gone, so the machine is untouched and the shared CLI/TUI service is never stopped, and the Proxy fields are now local draft state committed once by an explicit Apply button. On direction, I agree a server-read proxy config is the right long-term answer. I checked locally that Bun fetch does not reliably reroute when proxy env vars change at runtime, so doing this properly needs config schema plus protocol regeneration and HTTP-client changes in core. I would like a maintainer to confirm that direction before taking on that cross-package change, as you suggested. The PR description now states clearly that the setting affects the Electron session immediately and service processes started afterwards.

@opencodejr opencodejr Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I couldn't run this one: it needs the Electron app, which this environment doesn't have. From reading the code, the earlier points are all addressed:

  • The service restart is gone, along with its RPC and the per-field restarts.
  • Settings now save only when Apply is clicked.
  • The session-timing, mode: "system" and env-handling fixes from the previous commit are still in place.

The proxy tests in packages/desktop/src/main/native/ pass, and tsc -b is clean in packages/desktop and packages/app.

Public API: unchanged. The getProxySettings/setProxySettings platform methods, the app IPC RPCs and the proxySettings store key are all internal to Desktop.

One concern remains, inline. With the restart gone, the agent only gets the proxy if its background service happens to start after Apply. That service is shared and survives Desktop restarts, so the main case in the linked issue (webfetch through the proxy) may still not work. Before/after screenshots of the Proxy section would also help, since the UI is unverified.

"settings.general.row.pinchZoom.description": "Allow trackpad pinch and Ctrl-scroll gestures to zoom",
"settings.general.row.proxy.enabled.title": "Use a proxy",
"settings.general.row.proxy.enabled.description":
"Route network requests through a proxy. Apply to save; the proxy takes effect for the desktop app immediately and for new agent service processes.",

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With the restart gone, the agent only sees the proxy if the background service starts after Apply. That service is detached and shared with the CLI and TUI. Desktop doesn't stop it on quit and reuses it on the next launch, so in the usual case webfetch and model calls keep going direct even after Apply and a Desktop restart. Users can't see when a "new agent service process" happens, so the setting looks applied when it isn't, which is the main problem in #27892. Could the proxy be something the server reads itself (e.g. the opencode.json proxy key proposed in #24981, with Desktop writing it)? Then it would apply no matter who started the service. At minimum, the UI should say clearly that the agent won't use the proxy until the background service restarts, and how to do that.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fair point. In 287046a the Proxy section now shows a clear note when enabled: agent requests use the proxy only after the background service restarts, with the instruction to run opencode service restart. The shared service is left alone, so Desktop quit/relaunch does not silently change that. I agree a server-read config is the proper fix and have asked maintainers to confirm that direction before taking on the cross-package work.

@metif12

metif12 commented Nov 22, 2026

Copy link
Copy Markdown
Author

Third review addressed in 287046a: the Proxy section now states plainly that agent requests only use the proxy after the background service restarts, and gives the command to do it. No changes to the shared service. PR description updated to match, and app typecheck plus the 8 proxy unit tests still pass. Server-read proxy config remains the better fix; I am holding that cross-package change until maintainers weigh in.

@opencodejr opencodejr Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I couldn't run this one: it needs the Electron app, which this environment doesn't have. From reading the code, the new note says the agent won't use the proxy until the service restarts, which is the minimum I asked for in the open thread on this text.

The workaround it gives doesn't work, though. opencode service restart starts the new service from the terminal, so it never gets the proxy variables Desktop sets. Details are inline. The earlier thread about the shared service stays open for that reason.

Public API: unchanged. tsc -b is still clean in packages/app.

"settings.general.row.proxy.enabled.description":
"Route network requests through a proxy. Apply to save; the proxy takes effect for the desktop app immediately and for new agent service processes.",
"settings.general.row.proxy.apply": "Apply proxy settings",
"settings.general.row.proxy.serviceNote":

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This workaround won't pick up the proxy. opencode service restart (packages/cli/src/commands/handlers/service/restart.ts) stops the service and runs Service.ensure from the terminal's CLI process. The new service inherits that terminal's environment plus the CLI's service config env, not the HTTP(S)_PROXY that Desktop wrote into its own process.env. So a user who follows the note still gets a service without the proxy.

The CLI already stores a per-service environment (opencode service set env <name> <value>, in packages/cli/src/services/service-config.ts) and applies it when it starts the service. If a Desktop-started service reads it too, Desktop writing the proxy there would make it apply no matter who restarts the service. If not, the note shouldn't promise a fix that doesn't work.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are right, that command does not inherit the desktop env. I removed it in a9b66d1 and the note now only says that agent requests use the proxy when the background service starts with these settings, and that restarting Desktop does not restart the shared service. I did not write into the CLI service config from Desktop because that is a cross-package change and the direction is still with maintainers; making the note promise a working command without that would be wrong.

@metif12

metif12 commented Nov 22, 2026

Copy link
Copy Markdown
Author

Addressed the fourth review in a9b66d1: the broken opencode service restart instruction is gone. The UI now only states the limitation that agent requests use the proxy when the background service starts with these settings and that restarting Desktop does not restart the shared service. PR description updated to match. App typecheck is clean. The proper fix - Desktop writing proxy into the shared service config or the server reading a proxy setting itself - is still a cross-package change awaiting maintainer direction.

@opencodejr opencodejr Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Text-only update; I didn't run it. The misleading opencode service restart advice is gone.

The main concern from the open thread on the description text is still there. The note now says the agent "may keep going direct", and nothing in the change makes it use the proxy. That's the core of the linked issue. As it stands, users get a setting that applies to Desktop's own traffic and admits it may not reach the agent. Maintainers should decide whether that's acceptable here or whether the proxy should be a setting the server reads itself (e.g. #24981).

Public API: unchanged.

@gewangshang6-ops gewangshang6-ops left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

所有拉群新老请求合并

@opencodejr

opencodejr Bot commented Nov 22, 2026

Copy link
Copy Markdown
Contributor

Thanks for working through all of these. I checked the code at a9b66d1, and the session-timing, env-handling, restart-removal and Apply-button fixes are all in. Those threads, and the one on the opencode service restart note, are resolved. One of your replies still mentions saving respawning the service, but that's outdated since d8a43d3 removed the restart.

The only thread still open is the one about the shared service. The note is honest now, but the agent may still go direct, and that's the main ask in #27892. Whether to merge Desktop-only proxy handling or move to a proxy setting the server reads itself (e.g. #24981) is up to the maintainers, so I'll leave that thread for them.

@opencodejr opencodejr Bot mentioned this pull request Dec 1, 2026
1 task done

This branch has not been deployed

No deployments
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.

[Desktop][Windows] webfetch tool does not respect system proxy settings — feature request: add proxy config in settings UI

2 participants