Skip to content

Commit 832aa94

Browse files
authored
effect(worktree): migrate to AppProcess.run (anomalyco#27186)
1 parent 6c7f35b commit 832aa94

1 file changed

Lines changed: 20 additions & 30 deletions

File tree

packages/opencode/src/worktree/index.ts

Lines changed: 20 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,12 @@ import { errorMessage } from "../util/error"
1212
import { BusEvent } from "@/bus/bus-event"
1313
import { GlobalBus } from "@/bus/global"
1414
import { Git } from "@/git"
15-
import { Effect, Layer, Path, Schema, Scope, Context, Stream } from "effect"
16-
import { ChildProcess, ChildProcessSpawner } from "effect/unstable/process"
15+
import { Effect, Layer, Path, Schema, Scope, Context } from "effect"
16+
import { ChildProcess } from "effect/unstable/process"
1717
import { NodePath } from "@effect/platform-node"
1818
import { AppFileSystem } from "@opencode-ai/core/filesystem"
1919
import { BootstrapRuntime } from "@/effect/bootstrap-runtime"
20-
import { CrossSpawnSpawner } from "@opencode-ai/core/cross-spawn-spawner"
20+
import { AppProcess } from "@opencode-ai/core/process"
2121
import { InstanceState } from "@/effect/instance-state"
2222

2323
const log = Log.create({ service: "worktree" })
@@ -150,38 +150,35 @@ type GitResult = { code: number; text: string; stderr: string }
150150
export const layer: Layer.Layer<
151151
Service,
152152
never,
153-
| AppFileSystem.Service
154-
| Path.Path
155-
| ChildProcessSpawner.ChildProcessSpawner
156-
| Git.Service
157-
| Project.Service
158-
| InstanceStore.Service
153+
AppFileSystem.Service | Path.Path | AppProcess.Service | Git.Service | Project.Service | InstanceStore.Service
159154
> = Layer.effect(
160155
Service,
161156
Effect.gen(function* () {
162157
const scope = yield* Scope.Scope
163158
const fs = yield* AppFileSystem.Service
164159
const pathSvc = yield* Path.Path
165-
const spawner = yield* ChildProcessSpawner.ChildProcessSpawner
160+
const appProcess = yield* AppProcess.Service
166161
const gitSvc = yield* Git.Service
167162
const project = yield* Project.Service
168163
const store = yield* InstanceStore.Service
169164

170165
const git = Effect.fnUntraced(
171166
function* (args: string[], opts?: { cwd?: string }) {
172-
const handle = yield* spawner.spawn(
167+
const result = yield* appProcess.run(
173168
ChildProcess.make("git", args, { cwd: opts?.cwd, extendEnv: true, stdin: "ignore" }),
174169
)
175-
const [text, stderr] = yield* Effect.all(
176-
[Stream.mkString(Stream.decodeText(handle.stdout)), Stream.mkString(Stream.decodeText(handle.stderr))],
177-
{ concurrency: 2 },
178-
)
179-
const code = yield* handle.exitCode
180-
return { code, text, stderr } satisfies GitResult
170+
return {
171+
code: result.exitCode,
172+
text: result.stdout.toString("utf8"),
173+
stderr: result.stderr.toString("utf8"),
174+
} satisfies GitResult
181175
},
182-
Effect.scoped,
183176
Effect.catch((e) =>
184-
Effect.succeed({ code: 1, text: "", stderr: e instanceof Error ? e.message : String(e) } satisfies GitResult),
177+
Effect.succeed({
178+
code: 1,
179+
text: "",
180+
stderr: e instanceof Error ? e.message : String(e),
181+
} satisfies GitResult),
185182
),
186183
)
187184

@@ -461,18 +458,11 @@ export const layer: Layer.Layer<
461458
const runStartCommand = Effect.fnUntraced(
462459
function* (directory: string, cmd: string) {
463460
const [shell, args] = process.platform === "win32" ? ["cmd", ["/c", cmd]] : ["bash", ["-lc", cmd]]
464-
const handle = yield* spawner.spawn(
465-
ChildProcess.make(shell, args, { cwd: directory, extendEnv: true, stdin: "ignore" }),
461+
const result = yield* appProcess.run(
462+
ChildProcess.make(shell, args as string[], { cwd: directory, extendEnv: true, stdin: "ignore" }),
466463
)
467-
// Drain stdout, capture stderr for error reporting
468-
const [, stderr] = yield* Effect.all(
469-
[Stream.runDrain(handle.stdout), Stream.mkString(Stream.decodeText(handle.stderr))],
470-
{ concurrency: 2 },
471-
).pipe(Effect.orDie)
472-
const code = yield* handle.exitCode
473-
return { code, stderr }
464+
return { code: result.exitCode, stderr: result.stderr.toString("utf8") }
474465
},
475-
Effect.scoped,
476466
Effect.catch(() => Effect.succeed({ code: 1, stderr: "" })),
477467
)
478468

@@ -620,7 +610,7 @@ export const layer: Layer.Layer<
620610

621611
export const appLayer = layer.pipe(
622612
Layer.provide(Git.defaultLayer),
623-
Layer.provide(CrossSpawnSpawner.defaultLayer),
613+
Layer.provide(AppProcess.defaultLayer),
624614
Layer.provide(Project.defaultLayer),
625615
Layer.provide(AppFileSystem.defaultLayer),
626616
Layer.provide(NodePath.layer),

0 commit comments

Comments
 (0)