Feature hasn't been suggested before.
- I have verified this feature I'm about to request hasn't been suggested before.
Describe the enhancement you want to request
I run opencode serve as a long-lived server for an automation client. Each run hands the agent a directory, and when the run ends the client hands that directory to the next run. Before it does, it has to know that nothing from the finished run can still write there. Today there's no way to get that guarantee from the HTTP API.
Even when every read I can make comes back empty, something from the old run can still start up again:
- Waking a session after a notice is admitted.
Session.synthetic admits the inbox item, looks the session up again, and only then calls execution.wake (core/src/session/session.ts). Nothing bounds the time between those steps.
- Background shells. A shell drops off
GET /api/shell before its output is read, the job settles, and its notice is admitted (core/src/shell.ts, tool/plugin/shell.ts notifyWhenDone). DELETE /api/shell/:id removes it from the listing before the process has exited and before the notice has been produced.
- Subagent completion. It reads the child's messages and then admits the parent's notice in a separate step (
session/subagent-job.ts, session/subagent-completion.ts).
- Prompt preparation. It runs before the coordinator registers, so
GET /api/session/active doesn't show it.
So a client can poll session/active, shell and the inbox for every session in the directory, see nothing, hand the directory off, and then have a woken turn or a late notice start running there afterwards. The best workaround is to require the directory to look quiet for several seconds. That narrows the window but doesn't close it.
I'd like a location-scoped "close and drain" operation, for example POST /api/location/drain?location[directory]=…, that:
- stops new prompts, wakes and job starts for the location;
- cancels and waits for in-flight work: session executions (including preparation), subagent and shell jobs (process exit, not just leaving the listing), and notification producers;
- answers only once nothing in the location can run without a new client request, and reports anything it couldn't stop;
- optionally reopens the location afterwards, or leaves it fenced until the next request.
This gives automation clients, and anything else that hands a directory between agents (CI, schedulers, multi-agent orchestrators), one call that says the directory is quiet instead of a timing heuristic. It probably overlaps with how shutdown already tears down a location's shells and executions, but as a per-location operation that doesn't stop the server.
Related: #60107 (interrupting a session doesn't cancel its background shells), #60245, #48826.