From 3943b84ef00e83dbc3c48e6a92b02f0d88fb35f9 Mon Sep 17 00:00:00 2001 From: Luke Parker <10430890+Hona@users.noreply.github.com> Date: Thu, 15 Oct 2026 08:41:49 +1000 Subject: [PATCH] fix(cli): default the Windows service to port 45278 (#55547) --- packages/cli/src/services/service-config.ts | 11 +++++++- packages/cli/test/service.test.ts | 28 +++++++++++++++------ services/www/src/docs/content/cli/web.mdx | 19 ++++++++++++-- 3 files changed, 48 insertions(+), 10 deletions(-) diff --git a/packages/cli/src/services/service-config.ts b/packages/cli/src/services/service-config.ts index ff76f5d6c70..0c74f9fd16a 100644 --- a/packages/cli/src/services/service-config.ts +++ b/packages/cli/src/services/service-config.ts @@ -43,7 +43,16 @@ export function filename(channel = OPENCODE_CHANNEL) { return `service-${channel.replace(/[^a-zA-Z0-9._-]/g, "-")}.json` } -export function defaultPort(channel = OPENCODE_CHANNEL) { +export function defaultPort(channel = OPENCODE_CHANNEL, platform = process.platform) { + const port = channelPort(channel) + // Windows hands out 0xC000-0xFFFF for outgoing connections, and Hyper-V and WinNAT reserve blocks of that range at + // boot, so a port there can fail as in use while nothing listens on it. Moving into the 0xB000 block keeps the low + // 12 bits: 0xC0DE becomes 0xB0DE, which is 0xC0DE - 4096, a nod to the v1 port. + if (platform === "win32" && port >= 0xc000) return 0xb000 | (port & 0xfff) + return port +} + +function channelPort(channel: string) { if (channel === "latest" || channel === "dev" || channel === "beta" || channel === "next") return 0xc0de if (channel === "local") return 0xc0df return 10_000 + (Number.parseInt(Hash.fast(channel).slice(0, 8), 16) % 50_000) diff --git a/packages/cli/test/service.test.ts b/packages/cli/test/service.test.ts index 3992e34ec9c..27cb66e1dbe 100644 --- a/packages/cli/test/service.test.ts +++ b/packages/cli/test/service.test.ts @@ -18,13 +18,27 @@ import { testEffect } from "../../core/test/lib/effect" const it = testEffect(NodeFileSystem.layer) test("managed service ports are stable per installation channel", () => { - expect(ServiceConfig.defaultPort("latest")).toBe(0xc0de) - expect(ServiceConfig.defaultPort("dev")).toBe(0xc0de) - expect(ServiceConfig.defaultPort("beta")).toBe(0xc0de) - expect(ServiceConfig.defaultPort("next")).toBe(0xc0de) - expect(ServiceConfig.defaultPort("local")).toBe(0xc0df) - expect(ServiceConfig.defaultPort("preview-a")).toBe(ServiceConfig.defaultPort("preview-a")) - expect(ServiceConfig.defaultPort("preview-a")).not.toBe(ServiceConfig.defaultPort("preview-b")) + expect(ServiceConfig.defaultPort("latest", "linux")).toBe(0xc0de) + expect(ServiceConfig.defaultPort("dev", "linux")).toBe(0xc0de) + expect(ServiceConfig.defaultPort("beta", "linux")).toBe(0xc0de) + expect(ServiceConfig.defaultPort("next", "linux")).toBe(0xc0de) + expect(ServiceConfig.defaultPort("local", "linux")).toBe(0xc0df) + expect(ServiceConfig.defaultPort("latest", "darwin")).toBe(0xc0de) + expect(ServiceConfig.defaultPort("preview-a", "linux")).toBe(ServiceConfig.defaultPort("preview-a", "linux")) + expect(ServiceConfig.defaultPort("preview-a", "linux")).not.toBe(ServiceConfig.defaultPort("preview-b", "linux")) +}) + +test("managed service ports on Windows stay below the range Windows reserves", () => { + expect(ServiceConfig.defaultPort("latest", "win32")).toBe(0xc0de - 4096) + expect(ServiceConfig.defaultPort("dev", "win32")).toBe(0xc0de - 4096) + expect(ServiceConfig.defaultPort("local", "win32")).toBe(0xc0df - 4096) + const ports = Array.from({ length: 200 }, (_, index) => ({ + windows: ServiceConfig.defaultPort(`preview-${index}`, "win32"), + linux: ServiceConfig.defaultPort(`preview-${index}`, "linux"), + })) + expect(ports.some((port) => port.linux >= 0xc000)).toBe(true) + expect(ports.every((port) => port.windows >= 10_000 && port.windows < 0xc000)).toBe(true) + expect(ports.filter((port) => port.linux < 0xc000 && port.windows !== port.linux)).toEqual([]) }) test("service disabled accepts only booleans without changing configuration on invalid input", async () => { diff --git a/services/www/src/docs/content/cli/web.mdx b/services/www/src/docs/content/cli/web.mdx index 98d976db7c8..3c7326bbfcc 100644 --- a/services/www/src/docs/content/cli/web.mdx +++ b/services/www/src/docs/content/cli/web.mdx @@ -32,8 +32,23 @@ and open the printed link locally: $ ssh -L 49374:127.0.0.1:49374 my-server ``` -By default the server runs on port 49374 and listens only on localhost. You can -change this config with the `opencode service` command. +By default the server listens only on localhost. Its port depends on the +platform: + +| Platform | Default port | +| --------------------- | ------------ | +| macOS, Linux, and WSL | 49374 | +| Windows | 45278 | + +Windows reserves ports from 49152 up. Print the current address with +`opencode service status`: + +```bash +$ opencode service status +http://127.0.0.1:49374 +``` + +You can change this config with the `opencode service` command. ### Remote access