# Background work

> Run shell commands and subagents beside the conversation, follow and stop them, and keep jobs alive with a process supervisor.

Some work takes longer than a reply: a dev server, a long test suite, a
subagent researching one part of a task. zelk runs such work in the
background, beside the conversation, and the agent reads its output when it
needs it.

## What runs in the background

- **Shell commands.** Bash starts a command in the background when the agent
  asks for it (`run_in_background`), and moves a command there when it runs
  past its timeout. Whatever a command leaves running in its process group
  when it ends moves there too.
- **Subagents.** Task can start a subagent in the background; the agent
  goes on and collects the subagent's report later.

The agent follows this work with its own tools: `ProcessList`,
`ProcessOutput`, `ProcessInput` and `ProcessKill` for commands, `TaskOutput`
and `TaskStop` for subagents. The [built-in tools](/reference/tools/) list
them all.

## How long it lives

Background work belongs to the run that started it unless you asked for
more. By default it is stopped when that run's work ends. When you ask zelk
to keep something going across replies, such as a dev server, the agent
starts it for the whole conversation, and it stays until it is stopped or
the conversation closes. Either way, work zelk started on this machine ends
when zelk exits: a guard in each process group kills the group if the zelk
that owns it dies, even by `SIGKILL`. To keep jobs alive beyond that, use a
[process supervisor](#jobs-that-outlive-zelk).

## In the terminal UI

| Key or command | Does |
|---|---|
| Ctrl+B | while zelk works, move its running commands and subagents to the background; the reply goes on |
| Ctrl+Enter | send the message you queued now: running commands and subagents move to the background, and zelk reads your message next |
| ↓ on an empty prompt | open the background work, while the footer counts some running |
| **Background work…** (`/ps`) | the commands and subagents running and those that ended |

**Background work…** shows a command's output, and a subagent's
conversation live while it runs, and stops either one. It also lists the
agent's own browser, to close it, and your browser while the session holds
it, to let it go. When what runs can't move to the background, Ctrl+Enter
stops the turn under way instead.

Stopping a process cancels its pending completion notices before asking
the backend to stop it. If the backend cannot confirm the exit, the
process stays listed and you can stop it again. An idle session with a
run triggered only by cancelled notices ends that run on its next
execution without calling the model.

Deleting a session stops its background work first and cancels pending
completion notices from the selected subagents.

## Jobs that outlive zelk

A process supervisor owns commands independently of any zelk process, so a
job keeps running when you quit the terminal UI and comes back when you open
it again. Start the supervisor in the workspace, in a terminal of its own or
under a service manager:

```sh
mkdir -m 700 ~/.local/state/zelk-jobs
zelk processes -process-socket ~/.local/state/zelk-jobs/jobs.sock serve
```

The socket's directory must be private and outside the workspace. On Unix,
use mode 0700. On Windows, use a short path under your private NTFS profile
directory, for example `"$env:LOCALAPPDATA\zelk-jobs\jobs.sock"` in
PowerShell. The supervisor creates a missing directory with private
permissions. It refuses an active endpoint and can reuse the path after a
crash. Then start zelk with the same socket and confinement flags, such as
`-sandbox`:

```sh
zelk -process-socket ~/.local/state/zelk-jobs/jobs.sock
```

The agent can now start a command whose lifetime is persistent. Its input
and output stay with the supervisor when zelk exits, and a job that finishes
while zelk is closed reaches its conversation when zelk next opens it.
Stopping the supervisor kills its jobs. On Windows the supervisor owns each
command's Job Object. Start the supervisor independently of the client. An IDE
or terminal that puts the supervisor in a kill-on-close job can end it and its
commands when that enclosing job closes.

You can also manage the jobs yourself. These commands need no model:

```sh
SOCK=~/.local/state/zelk-jobs/jobs.sock
zelk processes -process-socket $SOCK list                     # process IDs and how each ended
zelk processes -process-socket $SOCK -id ID output            # its output (-cursor, -wait)
zelk processes -process-socket $SOCK -id ID input "yes"       # write to its standard input
zelk processes -process-socket $SOCK -id ID stop              # kill its process group
```

## Where to go deeper

- [`zelk processes`](/reference/cli/#zelk-processes) in the CLI reference
- [Background work and response completion](/internals/background/): every lifetime, and what happens on each kind of ending
