mirror of
https://github.com/anomalyco/opencode.git
synced 2026-11-19 17:01:37 +03:00
fix(desktop): give dev builds their own server and keep beta off dev servers (#58331)
This commit is contained in:
committed by
GitHub
Unverified
parent
d290006820
commit
8ee9351be9
@@ -4,7 +4,7 @@ import { OPENCODE_CHANNEL } from "./version"
|
||||
export function databasePath(data: string) {
|
||||
const filename =
|
||||
process.env.OPENCODE_DB ??
|
||||
(["latest", "dev", "beta", "next", "prod"].includes(OPENCODE_CHANNEL) ||
|
||||
(["latest", "beta", "next", "prod"].includes(OPENCODE_CHANNEL) ||
|
||||
process.env.OPENCODE_DISABLE_CHANNEL_DB === "1" ||
|
||||
process.env.OPENCODE_DISABLE_CHANNEL_DB === "true"
|
||||
? "opencode.db"
|
||||
|
||||
@@ -38,8 +38,11 @@ const decodeLegacy = Schema.decodeUnknownOption(
|
||||
)
|
||||
const decodeRegistration = Schema.decodeUnknownEffect(Schema.fromJsonString(Service.Info))
|
||||
|
||||
// Released channels share one service, port and database, so switching between stable and beta keeps the same sessions.
|
||||
// Dev builds come from every merge to dev and run their own: sharing let a dev build replace the stable server, and a
|
||||
// beta app then adopt that dev server.
|
||||
export function filename(channel = OPENCODE_CHANNEL) {
|
||||
if (channel === "latest" || channel === "dev" || channel === "beta" || channel === "next") return "service.json"
|
||||
if (channel === "latest" || channel === "beta" || channel === "next") return "service.json"
|
||||
return `service-${channel.replace(/[^a-zA-Z0-9._-]/g, "-")}.json`
|
||||
}
|
||||
|
||||
@@ -54,8 +57,9 @@ export function defaultPort(channel = OPENCODE_CHANNEL, platform = process.platf
|
||||
}
|
||||
|
||||
function channelPort(channel: string) {
|
||||
if (channel === "latest" || channel === "dev" || channel === "beta" || channel === "next") return 0xc0de
|
||||
if (channel === "latest" || channel === "beta" || channel === "next") return 0xc0de
|
||||
if (channel === "local") return 0xc0df
|
||||
if (channel === "dev") return 0xc0e0
|
||||
return 10_000 + (Number.parseInt(Hash.fast(channel).slice(0, 8), 16) % 50_000)
|
||||
}
|
||||
|
||||
@@ -125,7 +129,11 @@ const paths = Effect.gen(function* () {
|
||||
legacyConfigFile: legacy ? path.join(global.config, legacy) : undefined,
|
||||
legacyRegistrationFiles: [
|
||||
...(legacy ? [path.join(global.state, legacy)] : []),
|
||||
...(name !== "service.json" && OPENCODE_CHANNEL !== "local" ? [path.join(global.state, "service.json")] : []),
|
||||
// Dev never owned the shared registration, even while it wrote there; a dev server still in it belongs to an
|
||||
// older build and is left for a released client to replace.
|
||||
...(name !== "service.json" && OPENCODE_CHANNEL !== "local" && OPENCODE_CHANNEL !== "dev"
|
||||
? [path.join(global.state, "service.json")]
|
||||
: []),
|
||||
],
|
||||
configFile: path.join(global.config, name),
|
||||
}
|
||||
@@ -138,11 +146,7 @@ export const options = Effect.fnUntraced(function* (input: { readonly checkVersi
|
||||
file,
|
||||
version: input.checkVersion ? OPENCODE_VERSION : undefined,
|
||||
env: (yield* read()).env,
|
||||
command: [
|
||||
...selfCommand(),
|
||||
"serve",
|
||||
"--service",
|
||||
],
|
||||
command: [...selfCommand(), "serve", "--service"],
|
||||
}
|
||||
})
|
||||
|
||||
|
||||
@@ -19,7 +19,7 @@ const it = testEffect(NodeFileSystem.layer)
|
||||
|
||||
test("managed service ports are stable per installation channel", () => {
|
||||
expect(ServiceConfig.defaultPort("latest", "linux")).toBe(0xc0de)
|
||||
expect(ServiceConfig.defaultPort("dev", "linux")).toBe(0xc0de)
|
||||
expect(ServiceConfig.defaultPort("dev", "linux")).toBe(0xc0e0)
|
||||
expect(ServiceConfig.defaultPort("beta", "linux")).toBe(0xc0de)
|
||||
expect(ServiceConfig.defaultPort("next", "linux")).toBe(0xc0de)
|
||||
expect(ServiceConfig.defaultPort("local", "linux")).toBe(0xc0df)
|
||||
@@ -30,7 +30,7 @@ test("managed service ports are stable per installation channel", () => {
|
||||
|
||||
test("managed service ports on Windows stay below the ranges Windows and WSL reserve", () => {
|
||||
expect(ServiceConfig.defaultPort("latest", "win32")).toBe(0x70de)
|
||||
expect(ServiceConfig.defaultPort("dev", "win32")).toBe(0x70de)
|
||||
expect(ServiceConfig.defaultPort("dev", "win32")).toBe(0x70e0)
|
||||
expect(ServiceConfig.defaultPort("local", "win32")).toBe(0x70df)
|
||||
const ports = Array.from({ length: 200 }, (_, index) => ({
|
||||
windows: ServiceConfig.defaultPort(`preview-${index}`, "win32"),
|
||||
@@ -192,9 +192,9 @@ test("service config manages environment variables", async () => {
|
||||
}
|
||||
})
|
||||
|
||||
test("service filenames share release channels and identify preview channels", () => {
|
||||
test("service filenames share release channels and identify dev and preview channels", () => {
|
||||
expect(ServiceConfig.filename("latest")).toBe("service.json")
|
||||
expect(ServiceConfig.filename("dev")).toBe("service.json")
|
||||
expect(ServiceConfig.filename("dev")).toBe("service-dev.json")
|
||||
expect(ServiceConfig.filename("beta")).toBe("service.json")
|
||||
expect(ServiceConfig.filename("next")).toBe("service.json")
|
||||
expect(ServiceConfig.filename("local")).toBe("service-local.json")
|
||||
|
||||
@@ -3,7 +3,7 @@ import { Context, Effect, FileSystem, Layer, Option, Path, Schema } from "effect
|
||||
import { CHANNEL } from "../constants"
|
||||
import { BackgroundServiceState } from "./background-service-state"
|
||||
import { cleanStages } from "./cli-stages"
|
||||
import { keptStages, serviceVersion, shouldRetryEnsure } from "./cli-version"
|
||||
import { keptStages, serviceFile, serviceVersion, shouldRetryEnsure } from "./cli-version"
|
||||
import { DesktopCli } from "./desktop-cli"
|
||||
import { SidecarCredentials } from "./sidecar-credentials"
|
||||
import { sidecarProbe } from "./sidecar-probe"
|
||||
@@ -46,14 +46,11 @@ const connect = Effect.fn("BackgroundService.connect")(function* (mode: "initial
|
||||
if (isolated) process.env.XDG_STATE_HOME = app.getPath("userData")
|
||||
const client = yield* Effect.promise(() => import("@opencode/client/service"))
|
||||
|
||||
const file =
|
||||
process.env.OPENCODE_DESKTOP_SERVER_CHANNEL === "local"
|
||||
? path.join(
|
||||
process.env.XDG_STATE_HOME ?? path.join(app.getPath("home"), ".local", "state"),
|
||||
"opencode",
|
||||
"service-local.json",
|
||||
)
|
||||
: undefined
|
||||
const file = serviceFile({
|
||||
version: cli.version,
|
||||
server: process.env.OPENCODE_DESKTOP_SERVER_CHANNEL,
|
||||
stateHome: process.env.XDG_STATE_HOME ?? path.join(app.getPath("home"), ".local", "state"),
|
||||
})
|
||||
|
||||
const ensure = (required = version) =>
|
||||
client.Service.ensure({
|
||||
@@ -73,7 +70,8 @@ const connect = Effect.fn("BackgroundService.connect")(function* (mode: "initial
|
||||
|
||||
const runEnsure = () =>
|
||||
ensure().catch((error) => {
|
||||
if (!shouldRetryEnsure(version, error instanceof Error ? error.message : String(error))) throw error
|
||||
if (!shouldRetryEnsure(version === cli.version, error instanceof Error ? error.message : String(error)))
|
||||
throw error
|
||||
|
||||
return ensure(cli.version)
|
||||
})
|
||||
|
||||
@@ -1,15 +1,64 @@
|
||||
import { describe, expect, test } from "bun:test"
|
||||
import { keptStages, serviceVersion, shouldRetryEnsure } from "./cli-version"
|
||||
import path from "node:path"
|
||||
import { keptStages, releasedBuild, serviceFile, serviceVersion, shouldRetryEnsure } from "./cli-version"
|
||||
|
||||
describe("cli-version", () => {
|
||||
test.each([
|
||||
{ channel: "beta" as const, isolated: false, mode: "initial" as const, version: "0.0.0-beta-2", expected: undefined },
|
||||
{ channel: "beta" as const, isolated: true, mode: "initial" as const, version: "0.0.0-beta-2", expected: "0.0.0-beta-2" },
|
||||
{
|
||||
channel: "beta" as const,
|
||||
isolated: false,
|
||||
mode: "initial" as const,
|
||||
version: "0.0.0-beta-2",
|
||||
expected: releasedBuild,
|
||||
},
|
||||
{
|
||||
channel: "beta" as const,
|
||||
isolated: true,
|
||||
mode: "initial" as const,
|
||||
version: "0.0.0-beta-2",
|
||||
expected: "0.0.0-beta-2",
|
||||
},
|
||||
{ channel: "prod" as const, isolated: false, mode: "initial" as const, version: "2.0.32", expected: "2.0.32" },
|
||||
])("serviceVersion($channel, isolated=$isolated) -> $expected", (input) => {
|
||||
{
|
||||
channel: "dev" as const,
|
||||
isolated: false,
|
||||
mode: "initial" as const,
|
||||
version: "0.0.0-dev-9",
|
||||
expected: "0.0.0-dev-9",
|
||||
},
|
||||
{
|
||||
channel: "beta" as const,
|
||||
isolated: false,
|
||||
mode: "reconnect" as const,
|
||||
version: "0.0.0-beta-2",
|
||||
expected: undefined,
|
||||
},
|
||||
])("serviceVersion($channel, isolated=$isolated, $mode)", (input) => {
|
||||
expect(serviceVersion(input)).toBe(input.expected)
|
||||
})
|
||||
|
||||
// The beta app keeps whichever released server it finds in the shared registration, older or newer.
|
||||
test.each([
|
||||
{ running: "0.0.0-beta-1", expected: true },
|
||||
{ running: "0.0.0-beta-3", expected: true },
|
||||
{ running: "2.0.32", expected: true },
|
||||
{ running: "0.0.0-next-5", expected: true },
|
||||
// A dev build an older dev CLI registered in the shared file is replaced.
|
||||
{ running: "0.0.0-dev-21146", expected: false },
|
||||
])("releasedBuild($running) -> $expected", (input) => {
|
||||
expect(releasedBuild(input.running)).toBe(input.expected)
|
||||
})
|
||||
|
||||
// The file follows the CLI, not the app: a source build of the desktop runs whichever CLI it downloaded or built.
|
||||
test.each([
|
||||
{ version: "0.0.0-dev-9", server: undefined, expected: path.join("/state", "opencode", "service-dev.json") },
|
||||
{ version: "2.0.32", server: undefined, expected: undefined },
|
||||
{ version: "0.0.0-beta-2", server: undefined, expected: undefined },
|
||||
{ version: "local", server: "local", expected: path.join("/state", "opencode", "service-local.json") },
|
||||
])("serviceFile($version, server=$server)", (input) => {
|
||||
expect(serviceFile({ ...input, stateHome: "/state" })).toBe(input.expected)
|
||||
})
|
||||
|
||||
test.each([
|
||||
{ bundled: "0.0.0-beta-2", running: "0.0.0-beta-1", expected: ["0.0.0-beta-2-arm64", "0.0.0-beta-1-arm64"] },
|
||||
{ bundled: "0.0.0-beta-2", running: "0.0.0-beta-2", expected: ["0.0.0-beta-2-arm64"] },
|
||||
@@ -19,15 +68,16 @@ describe("cli-version", () => {
|
||||
})
|
||||
|
||||
test.each([
|
||||
{ version: undefined, message: "Background service failed to start", expected: true },
|
||||
{ version: undefined, message: "Timed out waiting for the background service to start", expected: true },
|
||||
{ pinned: false, message: "Background service failed to start", expected: true },
|
||||
{ pinned: false, message: "Timed out waiting for the background service to start", expected: true },
|
||||
{
|
||||
version: undefined,
|
||||
message: "Background service uses an incompatible health protocol. Update this client or explicitly restart the service.",
|
||||
version: releasedBuild,
|
||||
message:
|
||||
"Background service uses an incompatible health protocol. Update this client or explicitly restart the service.",
|
||||
expected: false,
|
||||
},
|
||||
{ version: "2.0.32", message: "Background service failed to start", expected: false },
|
||||
])("shouldRetryEnsure($version, $message) -> $expected", (input) => {
|
||||
expect(shouldRetryEnsure(input.version, input.message)).toBe(input.expected)
|
||||
{ pinned: true, message: "Background service failed to start", expected: false },
|
||||
])("shouldRetryEnsure($message) -> $expected", (input) => {
|
||||
expect(shouldRetryEnsure(input.pinned, input.message)).toBe(input.expected)
|
||||
})
|
||||
})
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
import path from "node:path"
|
||||
|
||||
export function parseCliVersion(output: string) {
|
||||
const marker = output.lastIndexOf(" v")
|
||||
const version = marker === -1 ? output : output.slice(marker + 2)
|
||||
@@ -15,14 +17,41 @@ export function serviceVersion(input: {
|
||||
}) {
|
||||
if (input.mode === "reconnect") return undefined
|
||||
|
||||
if (input.channel === "beta" && !input.isolated) return undefined
|
||||
// A beta update keeps the running server, so its sessions survive, but only a released build's: a dev build left in
|
||||
// the shared registration by an older CLI is replaced.
|
||||
if (input.channel === "beta" && !input.isolated) return releasedBuild
|
||||
|
||||
return input.version
|
||||
}
|
||||
|
||||
// Retrying on timeout can also replace a server stuck in a migration longer than 120 s, which is accepted.
|
||||
export function shouldRetryEnsure(version: string | undefined, message: string) {
|
||||
if (version !== undefined) return false
|
||||
/** Whether a server version comes from a released channel (stable, beta, next) rather than a dev build. */
|
||||
export function releasedBuild(version: string) {
|
||||
return !version.startsWith("0.0.0-dev-")
|
||||
}
|
||||
|
||||
/**
|
||||
* The registration file the CLI writes, or undefined for the shared `service.json` of the released channels. Follows
|
||||
* the CLI itself rather than the app's channel, because a source build can run any CLI: a dev build writes
|
||||
* `service-dev.json`, a released one `service.json`. Matches the CLI's `ServiceConfig.filename`.
|
||||
*/
|
||||
export function serviceFile(input: {
|
||||
/** The CLI's version. */
|
||||
readonly version: string
|
||||
/** `OPENCODE_DESKTOP_SERVER_CHANNEL`: `local` for a CLI built from this checkout. */
|
||||
readonly server?: string
|
||||
readonly stateHome: string
|
||||
}) {
|
||||
if (input.server === "local") return path.join(input.stateHome, "opencode", "service-local.json")
|
||||
|
||||
if (!releasedBuild(input.version)) return path.join(input.stateHome, "opencode", "service-dev.json")
|
||||
|
||||
return undefined
|
||||
}
|
||||
|
||||
// An ensure that accepted more than the bundled version retries pinned to it, replacing the failed server. Retrying on
|
||||
// timeout can also replace a server stuck in a migration longer than 120 s, which is accepted.
|
||||
export function shouldRetryEnsure(pinned: boolean, message: string) {
|
||||
if (pinned) return false
|
||||
|
||||
return (
|
||||
message === "Background service failed to start" ||
|
||||
|
||||
@@ -1,8 +1,10 @@
|
||||
import { readFileSync } from "node:fs"
|
||||
import { homedir } from "node:os"
|
||||
import path from "node:path"
|
||||
import { app } from "electron"
|
||||
import type { Endpoint } from "@opencode/client/service"
|
||||
import { CHANNEL } from "../constants"
|
||||
import { releasedBuild, serviceFile } from "./cli-version"
|
||||
|
||||
// The main thread idles between showing the first window and evaluating the main bundle, waiting
|
||||
// for the renderer's asset requests. That slot is long enough to find out whether a compatible
|
||||
@@ -13,11 +15,18 @@ let probe: Promise<Endpoint | undefined> | undefined
|
||||
|
||||
export function startSidecarProbe() {
|
||||
if (!app.isPackaged) return
|
||||
const version = CHANNEL === "beta" ? undefined : bundledVersion()
|
||||
const bundled = bundledVersion()
|
||||
const version = CHANNEL === "beta" ? releasedBuild : bundled
|
||||
|
||||
if (version === "") return
|
||||
|
||||
const file = serviceFile({
|
||||
version: bundled,
|
||||
stateHome: process.env.XDG_STATE_HOME ?? path.join(homedir(), ".local", "state"),
|
||||
})
|
||||
|
||||
probe = import("@opencode/client/service")
|
||||
.then(({ Service }) => Service.discover({ version }))
|
||||
.then(({ Service }) => Service.discover({ file, version }))
|
||||
.catch(() => undefined)
|
||||
}
|
||||
|
||||
|
||||
@@ -220,6 +220,15 @@ The database normally lives at:
|
||||
|
||||
`OPENCODE_DB` can override the database location.
|
||||
|
||||
Stable, beta and next share these files, so switching between them keeps your sessions. Dev builds run their own
|
||||
server with its own files, so they never replace or reuse the shared one:
|
||||
|
||||
| | Stable, beta, next | Dev builds |
|
||||
| ------------ | ------------------------ | ------------------------ |
|
||||
| Registration | `service.json` | `service-dev.json` |
|
||||
| Database | `opencode.db` | `opencode-dev.db` |
|
||||
| Default port | 49374 (28894 on Windows) | 49376 (28896 on Windows) |
|
||||
|
||||
<Callout type="warning">
|
||||
Do not delete or edit service files or the database while troubleshooting. Use the service commands to manage the
|
||||
daemon, and make a backup before inspecting persistent data with external tools.
|
||||
|
||||
Reference in New Issue
Block a user