Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions packages/desktop/src/main/service/background-service.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { app } from "electron"
import { Context, Effect, FileSystem, Layer, Path } from "effect"
import { BackgroundServiceState } from "./background-service-state"
import { cleanStages, DesktopCli } from "./desktop-cli"
import { cleanStages } from "./cli-stages"
import { DesktopCli } from "./desktop-cli"
import { SidecarCredentials } from "./sidecar-credentials"
import { sidecarProbe } from "./sidecar-probe"

Expand Down Expand Up @@ -76,7 +77,14 @@ const connect = Effect.fn("BackgroundService.connect")(function* (mode: "initial
...endpoint(url.origin),
})

if (mode === "initial" && isolated && cli.binary) yield* cleanStages(cli.binary).pipe(Effect.orDie)
// Only packaged and isolated builds run a staged copy; any other binary sits in the source tree.
// The service now runs the current version, so an older copy at most belongs to a process still exiting.
if (mode === "initial" && (app.isPackaged || isolated) && cli.binary)
runFork(
cleanStages(cli.binary).pipe(
Effect.catch((error) => Effect.logError("failed to clean staged v2 CLIs", { error })),
),
)
const ready = { url: url.origin, password: service.auth.password } satisfies SidecarCredentials.Data
SidecarCredentials.set(ready)

Expand Down
38 changes: 38 additions & 0 deletions packages/desktop/src/main/service/cli-stages.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { describe, expect, test } from "bun:test"
import { mkdirSync, mkdtempSync, readdirSync, rmSync, writeFileSync } from "node:fs"
import { tmpdir } from "node:os"
import path from "node:path"
import { NodeFileSystem, NodePath } from "@effect/platform-node"
import { Effect, Layer } from "effect"

import { cleanStages } from "./cli-stages"

describe("cleanStages", () => {
test.each([
{ name: "upgrade", staged: ["2.0.18", "2.0.19", "2.0.20"], current: "2.0.20" },
{ name: "downgrade", staged: ["2.0.18", "2.0.19", "2.0.20"], current: "2.0.18" },
{ name: "prerelease", staged: ["2.0.20", "2.1.0-beta.1", "local"], current: "2.1.0-beta.1" },
{ name: "only current", staged: ["2.0.20"], current: "2.0.20" },
])("keeps only the current staged CLI after $name", async (input) => {
const root = mkdtempSync(path.join(tmpdir(), "opencode-cli-stages-"))

try {
input.staged.forEach((version) => {
mkdirSync(path.join(root, version))
writeFileSync(path.join(root, version, "opencode-cli"), version)
})
writeFileSync(path.join(root, "unrelated"), "")

await Effect.runPromise(
cleanStages(path.join(root, input.current, "opencode-cli")).pipe(
Effect.provide(Layer.mergeAll(NodeFileSystem.layer, NodePath.layer)),
),
)

expect(readdirSync(root).sort()).toEqual([input.current, "unrelated"])
expect(readdirSync(path.join(root, input.current))).toEqual(["opencode-cli"])
} finally {
rmSync(root, { recursive: true, force: true })
}
})
})
27 changes: 27 additions & 0 deletions packages/desktop/src/main/service/cli-stages.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { Effect, FileSystem, Path } from "effect"

// Each version stages its own copy of the CLI, and only the current one is ever started again: a
// downgrade stages its copy anew. Windows refuses to delete an executable that has not exited yet,
// so a failed removal is logged and retried on the next launch.
export const cleanStages = Effect.fn("DesktopCli.cleanStages")(function* (binary: string) {
const fs = yield* FileSystem.FileSystem
const path = yield* Path.Path
const current = path.dirname(binary)
const root = path.dirname(current)
const entries = yield* fs.readDirectory(root)
yield* Effect.forEach(
entries,
Effect.fnUntraced(function* (entry) {
const target = path.join(root, entry)

if (target === current) return
const stat = yield* fs.stat(target).pipe(Effect.orElseSucceed(() => undefined))

if (stat?.type !== "Directory") return
yield* fs
.remove(target, { recursive: true, force: true })
.pipe(Effect.catch((error) => Effect.logError("failed to clean staged v2 CLI", { path: target, error })))
}),
{ concurrency: "unbounded" },
)
})
23 changes: 0 additions & 23 deletions packages/desktop/src/main/service/desktop-cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,29 +138,6 @@ function isVersionCache(value: unknown): value is VersionCache {
return typeof cache.path === "string" && typeof cache.identity === "string" && typeof cache.version === "string"
}

export const cleanStages = Effect.fn("DesktopCli.cleanStages")(function* (binary: string) {
const fs = yield* FileSystem.FileSystem
const path = yield* Path.Path
const current = path.dirname(binary)
const root = path.dirname(current)
const entries = yield* fs.readDirectory(root)
yield* Effect.forEach(
entries,
Effect.fnUntraced(function* (entry) {
const target = path.join(root, entry)

if (target === current) return
const stat = yield* fs.stat(target).pipe(Effect.orElseSucceed(() => undefined))

if (stat?.type !== "Directory") return
yield* fs
.remove(target, { recursive: true, force: true })
.pipe(Effect.catch((error) => Effect.logError("failed to clean staged v2 CLI", { path: target, error })))
}),
{ concurrency: "unbounded" },
)
})

const installCli = Effect.fn("DesktopCli.install")(function* (source: string, version: string) {
const fs = yield* FileSystem.FileSystem
const path = yield* Path.Path
Expand Down
Loading