# Language servers

An agent that edits a file learns whether it still compiles only when it
runs a build. With language servers, the Edit or Write tool's result
already says so:

```
Edited add.go: 1 replacement.

gopls reports 1 error in add.go:
  12:5 undefined: foo
```

This document says where the servers run, what a check waits for, and what
reaches the log. [`core/lsp`](https://github.com/barevoidco/zelk/blob/main/core/lsp/README.md) implements the
client; the file tools in `core/internal/builtin/files` call it; hosts
choose the servers.

## Where servers run

A server is a process started in the session's `env.Environment`, like the
agent's commands and stdio MCP servers: under the same sandbox, with the
same filesystem view and the environment the host gives it. A server that
the sandbox keeps off the network can't download modules; it checks with
what is there. zelk never installs a server.

The pool keeps one server per (workspace root, server). An isolated
subagent works in its own worktree, so it gets its own server; one working
in its parent's workspace shares the parent's. Servers start on the first
check of a file of their language and run until the agent closes, or, for
an isolated subagent, until each execution of it ends and its environment
closes; the next execution starts its own, with no stop counted against
it.

## A check

After a successful write, the tool checks the file as written:

1. The server is started and initialized, if it isn't already.
2. The file is opened with its new text and a version higher than any sent
   before.
3. The diagnostics come by pull (`textDocument/diagnostic`) where the server
   offers it; otherwise the pool waits for the server to publish that
   version's, then 250 ms more for later rounds, and takes the last.
4. The file is closed, so the server reads it from disk again: a buffer
   kept open would hide later changes, such as one by a command.

The whole check is bounded by a budget, 3 seconds by default
(`WithLanguageServerBudget` sets another), and closing
the file by a second more. A server still loading or too slow reports
nothing, and the result says nothing about the file: no news is not "no
errors". A server that stops reading its input holds a check no longer:
the write that fails ends its connection, and it counts as stopped.
Initialization goes on in the background, so a later check can use the
server.

## What the result says

Only errors are listed, at most 10, the rest counted; warnings and hints
would crowd out what matters. A clean file adds nothing. A server that
can't start, or stops twice, is named once in a result, then left alone for
the session.

The diagnostics are part of the tool's result, so they are in the log with
the call: a replayed or resumed session shows what the model was shown,
and the check doesn't run again.

## What isn't checked

- Files changed by Bash: the agent may run the build itself.
- Files in the session's scratch directory.
- Files of languages without a configured server.
- Native Windows: the hosts find servers on Linux and macOS.
