Description
This issue was written and posted by an LLM (Claude), with the permission of @skorokithakis.
Version: 2.3.2, opencode run --standalone --format json --auto
What happens
When the main session goes idle, opencode run lists the background jobs started during the prompt. If any remain, it prints a background_wait event and waits until all of them end. If the model starts a long-running process with background: true, such as a dev server, the job never ends. opencode run then never exits, even though the session is idle and its final reply is complete.
Reproduction
opencode run --standalone --format json --auto -- \
"Use the shell tool with background set to true to run exactly: sleep 400. Do not wait for it. Then reply with the single word: done."
The stream ends with:
{"type":"text",...,"text":"done",...}
{"type":"step_finish",...}
{"type":"background_wait",...,"jobs":[{"id":"sh_...","kind":"shell","title":"sleep 400"}]}
The process then stays alive until the job ends. With a server, it stays alive with no limit.
Why this matters
opencode run is the non-interactive entry point for scripts, CI and wrappers. Those callers have no way to put a limit on the wait. Their only option is to parse background_wait and kill the process themselves. We had to do this in a tool that runs OpenCode turns unattended. Before that, a 2-minute turn waited about 20 minutes for our watchdog and was then reported as failed.
Request
Add a flag to opencode run that controls this wait. For example:
--background=wait|detach|kill:waitis the current behaviour,detachexits and leaves the jobs running, andkillstops the jobs and exits; or--background-timeout <seconds>: after the limit, stop the remaining jobs and exit with the outcome of the session.
It would also help if the documentation for --format json described background_wait and its fields.
Activity
opencodejr commentedon Nov 27, 2026
Thanks for the detailed write-up and the reproduction. You're asking for a way to bound or skip the wait that
opencode rundoes for background jobs once the session is idle, for example--background=wait|detach|killor--background-timeout <seconds>, and for thebackground_waitevent to be documented under--format json. I've marked this as a feature request: the wait is howrunworks now, and I did not find a flag that changes it in the code I read. Maintainers will consider it.A possible workaround until then: send
SIGTERM(orSIGINT) instead of killing the process outright. The run then cancels its session and its background jobs (shell jobs are removed, subagent sessions are interrupted) and returns. So a wrapper that seesbackground_waitcould sendSIGTERMand get a clean exit without leaving the shell running.Related:
opencode runhangs when a child session is still busy as the root session finishes (fix(cli): stop waiting when an owned child session without a job ends #62643 is an open fix)opencode runalive until they finish, which makes this wait come up more oftenfix: Stop OpenCode turns that only wait on background shell jobs
Guembri01 commentedon Nov 27, 2026
Implemented the first shape you suggested, on branch
run-background-jobs(028127e).--background wait|detach|kill:waitstays the default, so nothing changes for existing callers.detachexits with the reply, leaves the jobs running, and the default format saysleaving N background jobs runninginstead of claiming it waits.killstops the shell and subagent jobs before exiting. Image, video and Code Mode jobs have no cancel through the API and are left to finish, which is the same set the SIGINT path already leaves.All three still emit
background_waitwithid,kindandtitle, so a JSON caller sees what was left behind; the two new modes end the stream right after it. The flag and the event are documented in the CLI reference next to--format json, which was the second half of your request.Tests in
packages/cli/test/run/noninteractive.test.tscover detach (noshell.remove, exits 0) and kill (shell.removewith the location,session.interrupton the subagent's child session, event still emitted); the file is 25 pass, 0 fail, including every existing background test.opencode run --helplists the flag.I did not add
--background-timeout; it is a different semantic (kill after a delay) and the three-value flag solves the CI hang without a timer. Happy to add it if maintainers prefer that shape.PR creation is returning 404 for this account right now, so it waits as a branch with the body written.