# Editors and ACP

> What the Agent Client Protocol is, how an editor drives zelk over it, and what zelk does with sessions, approvals, files and terminals when an editor is on the other end.

An agent in a terminal is one way to work. Many people would rather have it
in their editor, with its answers, its edits and its questions beside the
code. Once, each editor needed its own plugin for each agent. The Agent
Client Protocol, ACP, is an open standard for that gap. An editor that
speaks it can drive any agent that speaks it, so zelk needs no plugin for
each editor. `zelk acp` is how zelk speaks it.

The names can be the wrong way round if you think of the agent as being in
charge. The editor is the **client**: it owns the screen, the user and the
files. zelk is the **agent**: it owns the model and the work. The editor
starts zelk as a process, and the two talk over its standard input and
output.

## The conversation

The messages are JSON-RPC, one per line, as in [MCP](/concepts/mcp/): a
**request** with an id that is answered with a result or an error, and a
**notification** with no id that nothing answers. What is different is that
both sides send requests. The editor asks zelk to do things, and zelk asks
the editor things too.

A connection starts with `initialize`. The editor says which protocol
version it speaks and what it can offer: reading and writing files,
running terminals, showing forms. zelk answers with the version it speaks,
which is 1, and with what it can do, such as loading a session. There is no
sign-in step: the transport is the trust boundary.

Then the editor works with sessions:

- `session/new` makes a session for a working directory and returns its ID.
- `session/prompt` sends the user's message. It is a request, and it is
  answered only when the run is over, with a **stop reason**.
- While the run goes on, zelk sends `session/update` notifications: text as
  the model writes it, each tool call and its result, the plan.
- `session/cancel` is a notification that asks zelk to stop the run.
- `session/load` opens a session that already exists. zelk replays the
  conversation as updates, then answers.
- `session/request_permission` goes the other way. zelk asks the editor
  whether an action may run, and the editor's answer is the response.

```json
{"jsonrpc": "2.0", "id": 3, "method": "session/prompt",
 "params": {"sessionId": "…", "prompt": [{"type": "text", "text": "add a retry test"}]}}

{"jsonrpc": "2.0", "method": "session/update",
 "params": {"sessionId": "…", "update": {"sessionUpdate": "agent_message_chunk",
            "content": {"type": "text", "text": "Reading retry.go…"}}}}

{"jsonrpc": "2.0", "id": 3, "result": {"stopReason": "end_turn"}}
```

The scenes below play it: a prompt streaming back, an approval, a cancel,
and an editor coming back to a session after it closed. The editor is on
the left and zelk on the right, and time runs down. Each message is an
arrow that crosses when it is sent. Under the chart is what zelk writes in
the session's log, which is where most of the answers below come from.

Each scene is an editor and zelk joined by a wire. The messages, in the order they cross (→ from the editor to zelk, ← from zelk to the editor), and what zelk records:

- **A prompt**. A prompt is one request, and its answer is a stream. The editor sends session/prompt and waits. zelk streams the run as session/update notifications, and the request is answered when the run ends.
   Editor on the left, zelk on the right:
   1. → `session/prompt` add a retry test (id 1)
   2. ← `session/update` agent_message_chunk
   3. ← `session/update` tool_call
   4. ← `session/update` tool_call_update
   5. ← `session/update` agent_message_chunk
   6. ← `result` stopReason: end_turn (id 1)
   The session’s log:
   - `command_accepted` user_message: add a retry test
   - `run_started` run 1
   - `effect_started` run_tool Read retry.go
   - `effect_finished` Read retry.go → ok
   - `run_finished` success
- **Asking permission**. An approval is a request that goes the other way. zelk asks, the editor answers, and the user’s choice is the response. The session suspends in its log while it waits, so the question survives the connection.
   Editor on the left, zelk on the right:
   1. → `session/prompt` run the tests (id 1)
   2. ← `session/request_permission` Bash (id 1)
   3. → `result` selected: allow (id 1)
   4. ← `session/update` tool_call
   5. ← `session/update` tool_call_update
   6. ← `result` stopReason: end_turn (id 1)
   The session’s log:
   - `command_accepted` user_message: run the tests
   - `effect_started` await_input: approve Bash
   - `suspended` waiting on 1 request
   - `resumed`
   - `command_accepted` answer_input: allow
   - `effect_started` run_tool Bash go test ./...
   - `effect_finished` Bash go test ./... → ok
   - `run_finished` success
- **Cancelling**. A cancel has no answer of its own. session/cancel is a notification. zelk interrupts the run, and the prompt’s response comes back with the stop reason cancelled.
   Editor on the left, zelk on the right:
   1. → `session/prompt` rewrite the parser (id 1)
   2. ← `session/update` agent_message_chunk
   3. → `session/cancel`
   4. ← `result` stopReason: cancelled (id 1)
   The session’s log:
   - `command_accepted` user_message: rewrite the parser
   - `run_started` run 1
   - `effect_started` call_provider
   - `command_accepted` interrupt: cancelled by the client
   - `effect_cancelled` call_provider
   - `run_finished` interrupted
- **Coming back**. A session is a log, so a later editor can open it. The first editor closed with an approval unanswered. A new connection loads the session, sees its history replayed, and is asked the question again.
   Editor on the left, zelk on the right:
   - … the editor closes, the log keeps the request
   1. → `initialize` protocolVersion 1 (id 1)
   2. ← `result` loadSession: true (id 1)
   3. → `session/load` the session’s id (id 2)
   4. ← `session/update ×2` replays the history
   5. ← `result` modes, models (id 2)
   6. → `session/prompt` go on (id 3)
   7. ← `session/request_permission` Bash (id 1)
   8. → `result` selected: allow (id 1)
   The session’s log:
   - `command_accepted` user_message: make a file (already there)
   - `effect_started` await_input: approve Bash (already there)
   - `suspended` waiting on 1 request (already there)
   - `resumed`
   - `command_accepted` answer_input: allow

## How a session maps

An ACP session is a zelk session. `session/new` returns a fresh session ID,
and the session's log begins with the first prompt. Sessions are kept in
the state directory, the same way the terminal UI keeps its own, so the log
under the chart is the real record. A session's working directory is
the one `zelk acp` started in: a `session/new` that names another fails,
because an agent's environment is fixed when it is made.

Besides `new` and `load`, an editor can list the workspace's sessions,
newest first, and fork one into a copy of its conversation. A prompt
carries text, embedded text resources and images, for models that take
them. zelk also offers three kinds of slash command: `/compact`, `/status`,
and a skill by name.

What the editor sees while a run goes on:

- The model's text and its reasoning stream as message and thought chunks.
- A tool call is announced with a kind taken from the tool (`Read` is a
  read, `Bash` is an execute), then updated with its result. A result is
  cut at 16 KiB per update.
- `TodoWrite` becomes the plan.

A run ends with a stop reason. Success, and the ends that need a word of
explanation, are `end_turn`, and zelk follows an incomplete or blocked run
with a closing message that says what is left. A run that hits its turn
limit is `max_turn_requests`, one that hits its budget is `max_tokens`, a
refusal is `refusal`, and an interrupted run is `cancelled`. ACP version 1
has no stop reason for a failure, so a run that fails, such as one whose
provider can't be reached, fails the prompt instead, with an error that says
why and how to go on.

A prompt runs one at a time in a session. Cancelling interrupts the run, and
it also ends any approval or question zelk is waiting on, without carrying
out the action that was waiting.

## Approvals and questions

When the model asks for something that no rule allows, the run suspends, as
it does for any host that can't answer at once ([Permissions](/concepts/permissions/)).
`zelk acp` then asks the editor with `session/request_permission`. The
editor shows the choices: allow once, always allow when zelk has a rule to
suggest, and deny. A request the editor cancels cancels the action. A plan
awaiting approval comes the same way, as approve or keep planning.

The editor's answer goes into the log as an answer to the suspended request,
and the run takes it up from there. The session starts in `default` mode, so
anything that isn't a read asks. The editor's mode menu offers every mode
the session allows except `auto`, which needs a classifier policy you
chose.

A question the model asks the user comes to an editor that can show forms
as a form: a choice for each question, and a free-text field. To an editor
that can't, the question is shown as a message and the user's next prompt
is the answer.

## Files and terminals

At `initialize` the editor says whether it can read and write files and run
terminals. zelk doesn't use them unless you turn them on:

- `-client-fs` reads and writes files through the editor. The agent then
  sees unsaved buffers, and its edits appear in them. Whether a file exists
  is still the disk's to say, a write checks the file's revision just before
  it writes, and paths are checked against the workspace by their text, so
  this is a convenience for the editor and no fence. Glob and Grep read the
  disk.
- `-client-terminal` runs commands in the editor's terminals. They run
  outside zelk's sandbox and its network limits, they take no input, and
  they keep only the output the editor retains. A [managed policy](/cli/policy/)
  can turn the flag off, and one that requires a sandbox overrides it, since
  the editor's terminals are outside that sandbox.

Either is used only while the editor of that run is connected and offers
it. Without it, and after it disconnects, zelk uses its own environment.

The editor can also pass MCP servers with a session. zelk connects them for
that session, in the workspace and under the sandbox's launcher, as it does
servers from its own file ([MCP servers](/concepts/mcp/)).

## One zelk for many editors

By default, `zelk acp` is one process for one editor, and it ends when the
editor closes its input. `-listen` serves many editors from one zelk until
you stop it, on a Unix socket that only you can use or on a WebSocket on
the loopback address, where a client must send a token. The editor still
starts its agent as a command, so that command is `zelk acp -connect`,
which relays its standard input and output to the socket. All the clients
share one agent in one directory, a session one client started can be loaded
by another, and a second client's prompt to a session that is busy is
refused. [The ACP guide](/cli/acp/) has the commands.

## When the editor closes

Sessions are logs, so what an editor leaves behind is whatever the log
holds. An approval the editor never answered is a suspended session: the
request is recorded, and the next client that loads the session and prompts
it is asked again. Nothing waits on the old request: the log holds the
question, and the new prompt asks it afresh. Only one process writes a
session at a time, through its lease ([Durability and recovery](/concepts/durability/)), and a client of a
`-listen` server is one of many that share that process.

Whether a run goes on depends on whether the zelk process does. On stdio,
zelk exits with the editor, and closing the agent cancels a run in progress;
what it had recorded is in the log. Under `-listen`, the zelk process
outlives each editor, so a run goes on when its editor leaves, and the next
client to load the session takes it up. Either way, a session is never
lost with its editor.

## Limits

- zelk serves ACP version 1. It doesn't serve `session/resume`,
  `session/close` or `session/delete`: they answer method-not-found.
- A session works in the directory `zelk acp` started in, so one zelk
  serves one workspace.
- zelk accepts images in a prompt, but not audio. MCP servers over SSE
  aren't supported, and a session keeps the servers it started with:
  loading it with others fails.
- The WebSocket is the only remote transport zelk serves. It accepts only
  loopback hosts and refuses requests that come from a web page.
- Beyond ACP, zelk adds methods that start with `_zelk/` for clients that
  ask for them, such as following a session's log or steering a run. A
  standard editor never sees them. The TypeScript and Python SDKs that are
  planned are clients of `zelk acp` too, not ports of the loop.

## Where to go deeper

- [Editors through ACP](/cli/acp/): adding zelk to an editor, the flags and
  the listen modes
- [The loop](/concepts/loop/): the run that a prompt starts, and how it ends
- [Permissions](/concepts/permissions/): why an action asks, and what the
  editor's answer changes
- [Durability and recovery](/concepts/durability/): the log, the lease and
  what a crash leaves
- [The ACP contract](/internals/acp/): every method and how it maps onto
  zelk, with the limits and extensions in full
- [The embedding contract](/internals/embedding/): how an agent is served
  to a host's own connection
- In Go, the [`acp` package](/reference/go/acp/) serves an agent from your own
  host
