Skip to content

Commit a950fa2

Browse files
authored
fix(cli): rewrite noninteractive run and remove the V1 bridge (#61911)
1 parent 0dffb96 commit a950fa2

12 files changed

Lines changed: 1048 additions & 1118 deletions

File tree

‎packages/cli/package.json‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
],
1414
"exports": {
1515
"./vite-host": "./dev/host.js",
16-
"./run": "./src/run/index.ts",
1716
"./server-process": "./src/server-process.ts"
1817
},
1918
"scripts": {

‎packages/cli/src/commands/handlers/run.ts‎

Lines changed: 28 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -12,23 +12,33 @@ export default Runtime.handler(Commands.commands.run, (input) =>
1212
Effect.catch((error) => Effect.sync(() => reportRunError(input, error.message))),
1313
)
1414
if (!server) return
15-
yield* Effect.promise(() =>
16-
runNonInteractive({
17-
server,
18-
message: [...input.message],
19-
continue: input.continue,
20-
session: Option.getOrUndefined(input.session),
21-
fork: input.fork,
22-
worktree: Option.getOrUndefined(input.worktree),
23-
model: Option.getOrUndefined(input.model),
24-
agent: Option.getOrUndefined(input.agent),
25-
format: input.format,
26-
file: [...input.file],
27-
title: Option.getOrUndefined(input.title),
28-
archived: input.archived,
29-
thinking: input.thinking,
30-
auto: input.auto || input.yolo || input.dangerouslySkipPermissions,
31-
}),
32-
)
15+
// SIGINT and SIGTERM interrupt this fiber. Interruption waits for the run to cancel its Session and
16+
// jobs, so the private server and the process shut down only after that.
17+
yield* Effect.callback<void>((resume) => {
18+
const controller = new AbortController()
19+
const done = runNonInteractive(
20+
{
21+
server,
22+
message: [...input.message],
23+
continue: input.continue,
24+
session: Option.getOrUndefined(input.session),
25+
fork: input.fork,
26+
worktree: Option.getOrUndefined(input.worktree),
27+
model: Option.getOrUndefined(input.model),
28+
agent: Option.getOrUndefined(input.agent),
29+
format: input.format,
30+
file: [...input.file],
31+
title: Option.getOrUndefined(input.title),
32+
archived: input.archived,
33+
thinking: input.thinking,
34+
auto: input.auto || input.yolo || input.dangerouslySkipPermissions,
35+
},
36+
controller.signal,
37+
).then(() => resume(Effect.void))
38+
return Effect.promise(() => {
39+
controller.abort()
40+
return done
41+
})
42+
})
3343
}),
3444
)

‎packages/cli/src/index.ts‎

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import { ensurePluginRuntime } from "@opencode/plugin/runtime"
44
import { NodeRuntime, NodeServices } from "@effect/platform-node"
55
import { Cause, Effect } from "effect"
6-
import { getErrorReported } from "effect/Runtime"
6+
import { defaultTeardown, getErrorReported } from "effect/Runtime"
77
import { Commands } from "./commands/commands"
88
import { Runtime } from "./framework/runtime"
99
import { Observability } from "@opencode/util/observability"
@@ -39,6 +39,8 @@ if (process.env.OPENCODE_SSH_ASKPASS_PORT) {
3939
process.exit(await Effect.runPromise(askpass.pipe(Effect.provide(NodeServices.layer))))
4040
}
4141

42+
let terminated = false
43+
4244
const Handlers = Runtime.handlers(Commands, {
4345
$: () => import("./commands/handlers/default"),
4446
upgrade: () => import("./commands/handlers/upgrade"),
@@ -100,6 +102,10 @@ Effect.gen(function* () {
100102
yield* Heap.listen
101103
yield* CpuProfile.listen
102104
yield* ShutdownDeadline.install()
105+
// runMain reports every signal interruption as 130; remember a SIGTERM so the exit code is 143.
106+
process.once("SIGTERM", () => {
107+
terminated = true
108+
})
103109
const runFork = Effect.runForkWith(yield* Effect.context<never>())
104110
const uncaughtException = (cause: Error, origin: "uncaughtException" | "unhandledRejection") => {
105111
runFork(Effect.logError("uncaught exception", { cause, origin }))
@@ -152,7 +158,6 @@ Effect.gen(function* () {
152158
),
153159
Effect.provide(NodeServices.layer),
154160
Effect.scoped,
155-
Effect.tap(() => Effect.sync(() => process.exit(process.exitCode ?? 0))),
156161
// runMain's default reporter logs the fatal cause to stdout. Write it to stderr instead: the
157162
// desktop and `Service.ensure` only capture stderr from `serve --service`, so this is the only
158163
// channel through which a startup failure's reason reaches the user.
@@ -163,5 +168,16 @@ Effect.gen(function* () {
163168
process.stderr.write(Cause.pretty(cause) + "\n")
164169
}),
165170
),
166-
NodeRuntime.runMain({ disableErrorReporting: true }),
171+
NodeRuntime.runMain({
172+
disableErrorReporting: true,
173+
teardown: (exit) =>
174+
defaultTeardown(exit, (code) => {
175+
// Commands report failures through process.exitCode.
176+
const status = code === 0 ? (process.exitCode ?? 0) : code === 130 && terminated ? 143 : code
177+
// process.exit drops output still queued for a pipe; ending the streams waits for it.
178+
void Promise.all(
179+
[process.stdout, process.stderr].map((stream) => new Promise<void>((resolve) => stream.end(() => resolve()))),
180+
).then(() => process.exit(status))
181+
}),
182+
}),
167183
)

‎packages/cli/src/run/index.ts‎

Lines changed: 0 additions & 2 deletions
This file was deleted.

0 commit comments

Comments
 (0)