lsp
import "github.com/barevoidco/zelk/core/lsp"Package lsp checks files with language servers: after an agent edits a file, a Pool asks the server for its language what is wrong with it, so the agent learns of a compile error in the same tool result as the edit (docs/design/language-servers.md).
Servers run as processes in the session’s env.Environment, under the same sandbox as the agent’s commands. A server starts the first time a file of its language is checked, once per workspace root, and runs until the pool closes. A check is bounded by a budget: a server still loading, or one too slow, reports nothing rather than hold up the edit.
Constants
Section titled “Constants”const DefaultBudget = 3 * time.SecondDefaultBudget is how long a check waits for a server by default: long enough for a warm server to answer, short enough not to hold up an edit when it is still loading.
Diagnostic
Section titled “Diagnostic”type Diagnostic struct { // Line and Column are where it starts, from 1; Column counts UTF-16 // code units, as the protocol does. Line, Column int Severity Severity Message string // Source is what found it, such as "compiler", if the server says. Source string}Diagnostic is one problem a server found in a file.
Option
Section titled “Option”type Option func(*Pool)Option configures a Pool.
WithBudget
Section titled “WithBudget”func WithBudget(d time.Duration) OptionWithBudget sets how long a check waits for a server; DefaultBudget by default.
WithLogger
Section titled “WithLogger”func WithLogger(l *slog.Logger) OptionWithLogger sets where the pool logs servers starting and stopping.
type Pool struct { // contains filtered or unexported fields}Pool checks files with the servers it was given, starting each on first use. It is safe for concurrent use; checks of files that one server serves run one at a time.
NewPool
Section titled “NewPool”func NewPool(servers []Server, opts ...Option) (*Pool, error)NewPool returns a pool of servers. Each needs a name, a command with an absolute path, and at least one extension, which no other server claims.
Pool.Check
Section titled “Pool.Check”func (p *Pool) Check(ctx context.Context, e env.Environment, root, rel string, text []byte) (Report, error)Check asks the server for rel’s kind what is wrong with text, rel’s content, in the workspace root, starting the server in e if it isn’t running. rel is relative to root, which is the workspace’s absolute path as e’s processes see it. Check waits at most the pool’s budget, or until ctx is done, whose error it then returns; a server’s failure is never an error, only a report of nothing checked.
Example
A pool checks a Go file the agent just wrote with the server for Go, started in the session’s environment on first use.
gopls := lsp.Server{ Name: "gopls", Command: env.Cmd{Path: "/usr/local/bin/gopls"}, Languages: map[string]string{".go": "go"},}pool, err := lsp.NewPool([]lsp.Server{gopls})if err != nil { fmt.Println(err) return}defer pool.Close(context.Background())var e env.Environment = &fakeEnv{script: script{diagnose: undefinedFoo}} // the session's environmentr, err := pool.Check(context.Background(), e, "/work", "add.go", []byte("package add\n\nvar x = foo\n"))if err != nil { fmt.Println(err) return}for _, d := range r.Errors() { fmt.Printf("%s: %d:%d %s\n", r.Server, d.Line, d.Column, d.Message)}// Output: gopls: 3:9 undefined: fooOutput:
gopls: 3:9 undefined: fooPool.Close
Section titled “Pool.Close”func (p *Pool) Close(ctx context.Context) errorClose stops every server, waiting for each, and keeps any later check from starting one. ctx bounds asking each server to shut down; one that doesn’t is killed.
Pool.Release
Section titled “Pool.Release”func (p *Pool) Release(ctx context.Context, root string) errorRelease stops the servers of the workspace root, waiting for each, and forgets them: a later check there starts afresh, with nothing counted against it. A host calls it before closing an environment it checked files in, since a server that environment ends hasn’t stopped by itself. ctx bounds asking each server to shut down; one that doesn’t is killed.
Pool.Serves
Section titled “Pool.Serves”func (p *Pool) Serves(name string) boolServes reports whether a server checks files named like name.
Report
Section titled “Report”type Report struct { // Server names the server that checked the file; it is empty when no // server checks files of its kind, or one can't run. Server string // Checked says the server answered within the budget. When it didn't, // Diagnostics is empty and says nothing about the file. Checked bool Diagnostics []Diagnostic // Problem says, once, that the server can't check files any more: // it failed to start, or stopped twice. Problem string}Report is what a check found.
Report.Errors
Section titled “Report.Errors”func (r Report) Errors() []DiagnosticErrors returns the diagnostics of error severity.
Server
Section titled “Server”type Server struct { // Name names the server in reports, such as "gopls". Name string // Command starts it, speaking the protocol on its standard streams. // Its Dir is relative to the workspace root; its output is kept as // the pool reads it. Command env.Cmd // Languages maps the file extensions it checks, such as ".go", to the // protocol's language identifiers, such as "go". Languages map[string]string}Server is a language server a pool may start.
Severity
Section titled “Severity”type Severity intSeverity is how serious a diagnostic is, as the protocol numbers it.
const ( SeverityError Severity = 1 SeverityWarning Severity = 2 SeverityInformation Severity = 3 SeverityHint Severity = 4)The protocol’s severities.