Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
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
39 changes: 20 additions & 19 deletions packages/opencode/src/cli/cmd/run.ts
Original file line number Diff line number Diff line change
Expand Up @@ -784,26 +784,27 @@ export const RunCommand = effectCmd({
if (result.error) {
if (!emit("error", { error: result.error })) UI.error(formatRunError(result.error))
process.exitCode = 1
return
}
await finish()
return
}

const model = pick(args.model)
const result = await client.session.prompt({
sessionID,
agent,
model,
variant: args.variant,
parts: [...files, { type: "text", text: message }],
})
if (result.error) {
if (!emit("error", { error: result.error })) UI.error(formatRunError(result.error))
process.exitCode = 1
return
} else await finish()
} else {
const model = pick(args.model)
const result = await client.session.prompt({
sessionID,
agent,
model,
variant: args.variant,
parts: [...files, { type: "text", text: message }],
})
if (result.error) {
if (!emit("error", { error: result.error })) UI.error(formatRunError(result.error))
process.exitCode = 1
} else await finish()
}
await finish()
// The server keeps handles alive (db connections, sockets, timers),
// so a non-interactive run would otherwise hang with an idle event
// loop instead of terminating. Exit explicitly once it finishes,
// matching the error paths above. Attach mode drives a separate
// server, so leave it untouched.
if (!args.attach) process.exit(process.exitCode ?? 0)
return
}

Expand Down
17 changes: 17 additions & 0 deletions packages/opencode/test/cli/run/run-process.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,23 @@ describe("opencode run (non-interactive subprocess)", () => {
60_000,
)

// Regression for #32335: after a successful prompt the process used to stay
// alive with an idle event loop (server db connections, sockets and timers
// keep handles open), so scheduled `opencode run` jobs leaked and piled up.
// A successful run must terminate promptly on its own; a hang would expire
// the harness timeout and surface as a signal-killed failure instead.
cliIt.concurrent(
"exits promptly after a successful prompt instead of hanging (regression for #32335)",
({ llm, opencode }) =>
Effect.gen(function* () {
yield* llm.text("done")
const result = yield* opencode.run("say hi", { timeoutMs: 20_000 })
opencode.expectExit(result, 0)
expect(result.durationMs).toBeLessThan(20_000)
}),
30_000,
)

// --format json puts one JSON object per line on stdout for each emitted
// event. Consumers (CI scripts, tooling) parse this stream. Asserts the
// shape so a future event-emit change has to update this expectation.
Expand Down
Loading