On this page

0118: Shared tool servers and LSP overlays

Status: Proposed — design default, pending the maintainer's confirmation

Context

One tool process per agent defeats density, while concurrent edits from different workspaces must not be confused by a shared language server.

Decision

The runner registry keys entries by (kind, server id, canonical repo root, config hash), holding one sandboxed process, an agent refcount and an idle shutdown timer (tool_idle_ms, default 30000). Agent platform manifests declare [tool_server.<id>] with kind = "lsp"|"mcp", command, args and scope = "repo"|"runtime". Invalid declarations, including relative or missing commands and unknown kinds or scopes, are E0843; duplicate ids or declarations for the same (kind, repo) are E0844.

Use one LSP server per repo and Content-Length framing. Keep each agent's URI-to-(text, version) overlay and serialize requests through a per-server queue. Before an agent's request, send didChange for each touched URI to switch it to that agent's overlay, using monotonically increasing per-URI versions.

Consequences

Sharing limits process counts; overlay switching preserves each agent's view for a spec-compliant server but serializes requests. Owner confirmation of that v0 tradeoff is pending. Fixtures should verify process sharing and overlay switching without real servers or networking.

Alternatives

Per-agent servers consume more processes. Per-agent URI namespaces make servers index multiple copies of the same repository.

M13.3 step 19b implementation addendum (Proposed, pending maintainer)

The separate hrt_tools.h native implements the repo-keyed registry without extending run_input. Explicit sweep is the deterministic idle-shutdown hook; the production 30000 ms policy and language/manifest dispatch remain unwired. The M10.4 launcher supports persistent pipes: Linux/macOS reuse its sandboxed fork/exec path, canonical repo Fs root, network denial, scrubbed environment, direct argv and a private process group. Direct posix_spawn with the same environment scrub is used on other platforms. OWNER FLAG: tool servers not yet sandboxed on platforms other than Linux/macOS. Linux sandbox setup errors fail closed, never fall back to an unsandboxed spawn. The registry is runner-owned and serialized; adapters must drain protocol and stderr pipes. Sweeping zero-ref entries kills their process groups and reaps the leaders. Stats observe exit without releasing the pid until sweep.

The caller supplies validated configs (kind, id, absolute command, NULL-terminated args) on the registry. hrt_tools_acquire(registry, kind, id, repo_root, out) returns errno on failure. Sharing uses (kind, id, realpath(repo_root), SHA-256(command+args)); NUL-separated hash inputs preserve empty arguments and argv boundaries. Config storage is borrowed during acquire; entries own key snapshots. Eight references to one key share a process. hrt_tools_release(entry) decrements the refcount; zero-ref entries remain reusable until hrt_tools_sweep(registry), while referenced entries survive. hrt_tools_get_stats reports live processes and cumulative successful spawns. Exited servers are not reused (EPIPE); release and sweep before retry. Entry pointers expire on sweep; destroy only after releasing references and sweeping. The adapter pumps input/output/error pipes, including stderr draining and SIGPIPE/error handling; the native does not multiplex requests. The working directory is inherited, not changed to repo root. Runtime-scoped manifest dispatch and interpreter integration remain unwired.

M13.3 step 21 implementation addendum (Proposed, pending maintainer)

native/hrt_lsp.h attaches one adapter to each repo-scoped LSP registry entry. Bind on the runner before starting workers; acquire/release/sweep remain runner-serialized. Agent references keep the entry alive. Join users, destroy their overlays, release references, then sweep; sweep destroys adapter state and reaps the process. Do not share the raw stdio with another adapter.

The runtime API is hrt_lsp_bind(entry, out), hrt_lsp_edit(agent, uri, text, version) and hrt_lsp_request(agent, uri, line, column, diagnostics, out). Each agent starts with .server set and an empty document list. Edits require strictly increasing agent-local versions. These do not become server versions: the server owns a separate per-URI counter, starting at one for didOpen, then incrementing for every full-text didChange, including switches back to older agent-local versions. Unchanged text does not send another change. First use initializes the server and sends initialized; new documents are opened before their first change. Server versions stop at signed 32-bit maximum, rather than wrapping. Requests return malloc-owned JSON; failures return errno values.

A mutex/condition-variable ticket queue admits edits and requests FIFO by enqueue order, not by operating-system mutex wake order. It releases the admission mutex during IO while retaining exclusive ownership of the wire and server view until the response. Every request switches all documents in its agent's overlay, not just the requested URI. With no workspace/baseline provider on this branch, callers must explicitly seed each agent's full view of every shared touched URI. Missing entries fail with ENOENT before switching, never silently inherit another agent's text. A future workspace binding must supply baseline contents for untouched documents; implicit inheritance is rejected.

Content-Length frames and JSON nesting are bounded (1 MiB frame, depth 32, 4 KiB headers); edit strings are limited to 128 KiB each. IO uses nonblocking pipes with a five-second deadline per admitted request, drains stderr into a bounded 64 KiB log (discarding excess), and blocks/consumes only newly generated SIGPIPE in the calling thread before restoring its mask. Wire failures poison the adapter until release/sweep, rather than reusing uncertain server state.

Hover is a JSON-RPC request. diagnostics=1 issues a hover barrier and returns the latest publishDiagnostics notification for that URI in the current view; no notification yields ENODATA. Cache entries are invalidated on text switches. This establishes isolation for the fixture's ordered synchronous push behavior, not general asynchronous LSP diagnostic completion: a real server can publish unversioned old diagnostics after the barrier. Capability negotiation, arbitrary server-initiated requests and diagnostic pull are not implemented. Real-server interoperability and version-aware asynchronous diagnostic routing remain open; do not expose this as a general spec-compliant LSP client yet.

The unchanged fake_lsp.py implements hover and pushed diagnostics, but neither diagnostic pull nor stderr counters. The C test wraps its real read_message with PID/request/version instrumentation and a strict version assertion. It checks two divergent agents, four concurrent callers, known FIFO enqueue order, multiple/escaped/Unicode URIs, unchanged-text suppression, stale edits, a single process, malformed/truncated frames, bounded noisy-stderr draining, broken pipes and synchronous reap under ASan/UBSan and TSan.

Open language-port gap: as in ADR-0119 step 20, no existing native supplies a persistent registry handle and interactive stdio to Harness. Process.run_input is EOF-buffered. No new natives or pretend language port are added; manifest dispatch, language-level lsp and interpreter integration remain unwired.