effect(installation): migrate to AppProcess.run (#27188)

This commit is contained in:
Kit Langton
2026-05-13 11:54:29 -04:00
committed by GitHub
parent e5319846ad
commit 5cdbb7505e
2 changed files with 204 additions and 213 deletions
+30 -41
View File
@@ -1,8 +1,8 @@
import { Effect, Layer, Schema, Context, Stream } from "effect" import { Effect, Layer, Schema, Context, Stream } from "effect"
import { FetchHttpClient, HttpClient, HttpClientRequest, HttpClientResponse } from "effect/unstable/http" import { FetchHttpClient, HttpClient, HttpClientRequest, HttpClientResponse } from "effect/unstable/http"
import { CrossSpawnSpawner } from "@opencode-ai/core/cross-spawn-spawner"
import { withTransientReadRetry } from "@/util/effect-http-client" import { withTransientReadRetry } from "@/util/effect-http-client"
import { ChildProcess, ChildProcessSpawner } from "effect/unstable/process" import { ChildProcess } from "effect/unstable/process"
import { AppProcess } from "@opencode-ai/core/process"
import path from "path" import path from "path"
import { BusEvent } from "@/bus/bus-event" import { BusEvent } from "@/bus/bus-event"
import { Flag } from "@opencode-ai/core/flag/flag" import { Flag } from "@opencode-ai/core/flag/flag"
@@ -85,47 +85,43 @@ export interface Interface {
export class Service extends Context.Service<Service, Interface>()("@opencode/Installation") {} export class Service extends Context.Service<Service, Interface>()("@opencode/Installation") {}
export const layer: Layer.Layer<Service, never, HttpClient.HttpClient | ChildProcessSpawner.ChildProcessSpawner> = export const layer: Layer.Layer<Service, never, HttpClient.HttpClient | AppProcess.Service> = Layer.effect(
Layer.effect(
Service, Service,
Effect.gen(function* () { Effect.gen(function* () {
const http = yield* HttpClient.HttpClient const http = yield* HttpClient.HttpClient
const httpOk = HttpClient.filterStatusOk(withTransientReadRetry(http)) const httpOk = HttpClient.filterStatusOk(withTransientReadRetry(http))
const spawner = yield* ChildProcessSpawner.ChildProcessSpawner const appProcess = yield* AppProcess.Service
const text = Effect.fnUntraced( const text = Effect.fnUntraced(
function* (cmd: string[], opts?: { cwd?: string; env?: Record<string, string> }) { function* (cmd: string[], opts?: { cwd?: string; env?: Record<string, string> }) {
const proc = ChildProcess.make(cmd[0], cmd.slice(1), { const result = yield* appProcess.run(
ChildProcess.make(cmd[0], cmd.slice(1), {
cwd: opts?.cwd, cwd: opts?.cwd,
env: opts?.env, env: opts?.env,
extendEnv: true, extendEnv: true,
}) }),
const handle = yield* spawner.spawn(proc) )
const out = yield* Stream.mkString(Stream.decodeText(handle.stdout)) return result.stdout.toString("utf8")
yield* handle.exitCode
return out
}, },
Effect.scoped,
Effect.catch(() => Effect.succeed("")), Effect.catch(() => Effect.succeed("")),
) )
const run = Effect.fnUntraced( const run = Effect.fnUntraced(
function* (cmd: string[], opts?: { cwd?: string; env?: Record<string, string> }) { function* (cmd: string[], opts?: { cwd?: string; env?: Record<string, string> }) {
const proc = ChildProcess.make(cmd[0], cmd.slice(1), { const result = yield* appProcess.run(
ChildProcess.make(cmd[0], cmd.slice(1), {
cwd: opts?.cwd, cwd: opts?.cwd,
env: opts?.env, env: opts?.env,
extendEnv: true, extendEnv: true,
}) }),
const handle = yield* spawner.spawn(proc)
const [stdout, stderr] = yield* Effect.all(
[Stream.mkString(Stream.decodeText(handle.stdout)), Stream.mkString(Stream.decodeText(handle.stderr))],
{ concurrency: 2 },
) )
const code = yield* handle.exitCode return {
return { code, stdout, stderr } code: result.exitCode,
stdout: result.stdout.toString("utf8"),
stderr: result.stderr.toString("utf8"),
}
}, },
Effect.scoped, Effect.catch(() => Effect.succeed({ code: 1, stdout: "", stderr: "" })),
Effect.catch(() => Effect.succeed({ code: ChildProcessSpawner.ExitCode(1), stdout: "", stderr: "" })),
) )
const getBrewFormula = Effect.fnUntraced(function* () { const getBrewFormula = Effect.fnUntraced(function* () {
@@ -136,27 +132,23 @@ export const layer: Layer.Layer<Service, never, HttpClient.HttpClient | ChildPro
return "opencode" return "opencode"
}) })
const upgradeCurl = Effect.fnUntraced( const upgradeCurl = Effect.fnUntraced(function* (target: string) {
function* (target: string) {
const response = yield* httpOk.execute(HttpClientRequest.get("https://opencode.ai/install")) const response = yield* httpOk.execute(HttpClientRequest.get("https://opencode.ai/install"))
const body = yield* response.text const body = yield* response.text
const bodyBytes = new TextEncoder().encode(body) const bodyBytes = new TextEncoder().encode(body)
const proc = ChildProcess.make("bash", [], { const result = yield* appProcess.run(
ChildProcess.make("bash", [], {
stdin: Stream.make(bodyBytes), stdin: Stream.make(bodyBytes),
env: { VERSION: target }, env: { VERSION: target },
extendEnv: true, extendEnv: true,
}) }),
const handle = yield* spawner.spawn(proc)
const [stdout, stderr] = yield* Effect.all(
[Stream.mkString(Stream.decodeText(handle.stdout)), Stream.mkString(Stream.decodeText(handle.stderr))],
{ concurrency: 2 },
)
const code = yield* handle.exitCode
return { code, stdout, stderr }
},
Effect.scoped,
Effect.orDie,
) )
return {
code: result.exitCode,
stdout: result.stdout.toString("utf8"),
stderr: result.stderr.toString("utf8"),
}
}, Effect.orDie)
const result: Interface = { const result: Interface = {
info: Effect.fn("Installation.info")(function* () { info: Effect.fn("Installation.info")(function* () {
@@ -257,7 +249,7 @@ export const layer: Layer.Layer<Service, never, HttpClient.HttpClient | ChildPro
return data.tag_name.replace(/^v/, "") return data.tag_name.replace(/^v/, "")
}, Effect.orDie), }, Effect.orDie),
upgrade: Effect.fn("Installation.upgrade")(function* (m: Method, target: string) { upgrade: Effect.fn("Installation.upgrade")(function* (m: Method, target: string) {
let upgradeResult: { code: ChildProcessSpawner.ExitCode; stdout: string; stderr: string } | undefined let upgradeResult: { code: number; stdout: string; stderr: string } | undefined
switch (m) { switch (m) {
case "curl": case "curl":
upgradeResult = yield* upgradeCurl(target) upgradeResult = yield* upgradeCurl(target)
@@ -320,10 +312,7 @@ export const layer: Layer.Layer<Service, never, HttpClient.HttpClient | ChildPro
}), }),
) )
export const defaultLayer = layer.pipe( export const defaultLayer = layer.pipe(Layer.provide(FetchHttpClient.layer), Layer.provide(AppProcess.defaultLayer))
Layer.provide(FetchHttpClient.layer),
Layer.provide(CrossSpawnSpawner.defaultLayer),
)
const { runPromise } = makeRuntime(Service, defaultLayer) const { runPromise } = makeRuntime(Service, defaultLayer)
@@ -4,6 +4,7 @@ import { HttpClient, HttpClientRequest, HttpClientResponse } from "effect/unstab
import { ChildProcess, ChildProcessSpawner } from "effect/unstable/process" import { ChildProcess, ChildProcessSpawner } from "effect/unstable/process"
import { Installation } from "../../src/installation" import { Installation } from "../../src/installation"
import { InstallationChannel } from "@opencode-ai/core/installation/version" import { InstallationChannel } from "@opencode-ai/core/installation/version"
import { AppProcess } from "@opencode-ai/core/process"
import { testEffect } from "../lib/effect" import { testEffect } from "../lib/effect"
const encoder = new TextEncoder() const encoder = new TextEncoder()
@@ -47,7 +48,8 @@ function testLayer(
httpHandler: (request: HttpClientRequest.HttpClientRequest) => Response, httpHandler: (request: HttpClientRequest.HttpClientRequest) => Response,
spawnHandler?: (cmd: string, args: readonly string[]) => string, spawnHandler?: (cmd: string, args: readonly string[]) => string,
) { ) {
return Installation.layer.pipe(Layer.provide(mockHttpClient(httpHandler)), Layer.provide(mockSpawner(spawnHandler))) const appProcess = AppProcess.layer.pipe(Layer.provide(mockSpawner(spawnHandler)))
return Installation.layer.pipe(Layer.provide(mockHttpClient(httpHandler)), Layer.provide(appProcess))
} }
describe("installation", () => { describe("installation", () => {