diff options
| author | Gabriel Schneider <[email protected]> | 2026-11-26 04:54:39 -0300 |
|---|---|---|
| committer | Gabriel Schneider <[email protected]> | 2026-11-26 06:12:14 -0300 |
| commit | 02b64ff90ae2f82fb83a604f6d4ded3459a84620 (patch) | |
| tree | 066e398aac05727d7b64d3413fdbe92f6c204fcc /dev/lsp/lsp_client.zig | |
| parent | 5dab435d7262b87d2cf40d6a27cea9c2d981c2e8 (diff) | |
| download | pardes-02b64ff90ae2f82fb83a604f6d4ded3459a84620.tar.gz pardes-02b64ff90ae2f82fb83a604f6d4ded3459a84620.zip | |
Lsp, lsp_client, File, dump: a file of a language a server speaks, opened in a pane (a Look, a landing, a walk step, the boot's file, a restored session's), starts that server and tells it of the file on a worker of its own, so the file's first question finds it ready (agent V4's find for the comparison video, VIDEO2/STORY.md §4.1 and §10.2: pardes's first `gd` cost about 2.5 s where Helix's, LazyVim's and Doom's cost 0.05 s; the user was asked, the default yes); THE CAUSE: dev/lsp/lsp_client.zig spawned a server on the first query that needed it (its header's lifecycle), and that query paid the spawn, the handshake and the server's first parse of the file; THE FIX: an effect `lsp_open`, emitted beside the file's watch by File.open (look.open, Trail's step, the boot document) and dump.revivePane (a restored or upgraded session), performed through a Host entry `lsp_open` (tui, gui and macOS: Inbox.openLsp; not held for `threads_ok`: the boot's file and a restored session's are opened before the loop's threads start); dev/Lsp.zig `open` asks nothing for a file no spec speaks for (lsp.speaks, on the loop), else copies the pane's file (`snapshot`) and hands it to a worker of its own (`Opener`, one at a time: a file opened while one is under way is let go and told at its first query, syncDoc; one done is awaited, at once, before the next), which calls lsp_client.open: a spec disabled asks nothing, a binary not found disables the spec without a word (its first query says `language server for .py not found`, not every file opened), else `ensure` (the server started as a query starts it, `starting` and `ready` narrated as before, one per spec) and `syncDoc` (didOpen); nothing in the query log; not through Lsp.start (its cancel and join of the query in flight waits out a protected handshake, up to init_ms = 8 s, on the loop) nor `work` (the queries' one fixed region, memory.takeLspQuery); a picker's preview (Pick.preview, no pane) and a 9P `ctl get` (ninep/ctl.zig getFile, no File.open) start none; Lspinfo says `not started yet (a file of its opened starts it)`; docs/typ/setup.typ and reference.typ say when a server starts; THE HARNESSES (the coordinator's ruling: no test starts a server of this machine's): PARDES_LSP_OPEN=off starts none and is the process harnesses' default (test/process.zig's child environment, test/tty.zig, and test/fs.py, selfmount.py and daemon.py's base_env), so the `lsp` tty cases' zls is started by their first query as before; a test binary (builtin.is_test) starts none unless it says `on`, as the behaviour harness's language sessions do (Session.initLanguage, every spec the mock or off, `.lsp_open` drained as a host performs it, awaited); RUN (TEST FIRST): behaviour/language_servers.zig "a file of a server's language opened in a pane starts that server and tells it of the file, with no query asked; a picker's preview of one starts none, a second file no second server, and a server not installed is found silently" previews a.zig in Find's picker (Lspinfo: zls `not started yet`), opens it (zls `ready`, `docs 1`, the query count unchanged), opens b.zig (`docs 2`: the same server), and opens c.py and d.py with PARDES_LSP_PY naming no binary (pyright `disabled`, no query, nothing on the screen): fails on the base at language_servers.zig:367 (zls not started by the open), passes now; lsp_client.zig "a file opened whose server is not installed disables it without a word ..." opens two .py files with a status sink set: the spec disabled, no word posted, a query then `not found`; MEASURED (.scratch/walkui/T23/measure-v8.sh: VIDEO's probe.py, the screen polled every 50 ms, this change's Debug tui, a reflink copy of VIDEO2's V8 tree with clangd and its 27-file compile database, torque-parser.cc opened, `743ggfI` then `gd` 4 s after the start, until utils.h's `bool IsUpperCamelCase` shows): with PARDES_LSP_OPEN=off (the base's way) 1.97, 1.98, 1.97 s; started at the open 0.05, 0.05, 0.05 s; on VIDEO's tally with zls (measure.sh) both ways 0.05-0.10 s (zls starts and answers within one poll there); a user hit it on the first question of every file of a session's languages, about 2 s with clangd over a real project; NEW HOT PATHS: none per key or frame; per file opened, one effect more and on the loop lsp.speaks (the spec table's extensions); for a file a spec speaks for, a copy of its text (the snapshot a query makes) and a worker started; GATES: core-test 1130/1304 (174 skipped: +1), shell-test 1352/1355 (3 skipped: +1), pane-test 183/184, behaviour-test 286/286 (+1) and --replay 20 seeds of 70 steps, 0 failures, process-test 0 of 147 failed (3 skipped), term-test 17/17, kitty-test 45/45; the wire's semantics move (a Host entry, tape.shape); no dump change (effects are local, not dumped); the soak `replicas -- --clients 5 --rtt 30,80,207` dies on the base too (5dab435d7262, run here: `allocator leaks detected` at seed 0 rtt 80, MuPDF's allocations of documents a picker previewed, Pick.showKind), so run without the pdf addon (its binary, `--addon` image and life): diverged 0, unequal 0, quiet checks 468/0 251/0 657/0, stops not on may_wait 0, the corpus 22 of 22 scenes the same, no leak (a first run had one key stop at `spawn` at seed 3 rtt 80, exec.zig's rerun of a command pane, which emits a spawn with no `open a terminal` stop; the base's run and a second run had none); the gui unit-test run once, on T3's change over this one (its description); no frame or perf gate (the user's rule of 2026-11-26); load 1.1 to 9.1
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_016Y9MNBFhdevVu48bvL9kCE
Diffstat (limited to 'dev/lsp/lsp_client.zig')
| -rw-r--r-- | dev/lsp/lsp_client.zig | 88 |
1 files changed, 81 insertions, 7 deletions
diff --git a/dev/lsp/lsp_client.zig b/dev/lsp/lsp_client.zig index a53ff57f..be5069ae 100644 --- a/dev/lsp/lsp_client.zig +++ b/dev/lsp/lsp_client.zig @@ -6,9 +6,10 @@ //! //! THE PROCESS LIFECYCLE IS THE DESIGN. `lsp.query` is a synchronous call on a //! worker thread, and a language server costs tens of milliseconds to start -//! and MINUTES to index a large workspace. So a server is spawned lazily on -//! the first query that needs it, kept for the life of the editor, and each -//! spec gets at most one — `conns[i]` guards itself with an `Io.Mutex` +//! and MINUTES to index a large workspace. So a server is spawned when a +//! file of its language opens in a pane (`open`, on a worker of its own) or +//! by the first query that needs it, kept for the life of the editor, and +//! each spec gets at most one — `conns[i]` guards itself with an `Io.Mutex` //! because a canceled query can still be inside `run` when the next arrives. //! Every wait is an `Io` cancelation point: a shell cancels the worker of a //! query it no longer wants, and the worker stops there. @@ -40,7 +41,8 @@ //! (ignoring SIGPIPE process-wide would be inherited by every pty shell we //! fork and would change what `yes | head` does in a pane), //! - a binary missing from PATH costs one cached probe, disables the spec -//! for the session, and says so ONCE on the message row. +//! for the session, and says so ONCE on the message row (a query's; a +//! file opened says nothing). //! //! Position encoding is negotiated to utf-8 and the SERVER'S ANSWER is //! believed, not our request; the utf-16 conversion is implemented in both @@ -478,8 +480,8 @@ fn status(io: std.Io, path: []const u8, out: *std.Io.Writer) !void { const thier = c.caps.type_hier; c.mu.unlock(io); const env_v = if (libc.getenv(s.env)) |v| std.mem.span(v) else null; - // Never spawned is not off: the first query of its files starts it. - try out.print(" {s:<28} {s}", .{ s.name, if (state == .off) "not started yet (its first query starts it)" else @tagName(state) }); + // Never spawned is not off: a file of its opened starts it. + try out.print(" {s:<28} {s}", .{ s.name, if (state == .off) "not started yet (a file of its opened starts it)" else @tagName(state) }); if (state == .ready or state == .starting) { try out.print(" root {s} {s}", .{ root, @tagName(enc) }); if (pull) try out.print(" pull-diags", .{}); @@ -757,6 +759,36 @@ pub fn query(io: std.Io, arena: std.mem.Allocator, asked: lsp.Req, out: *std.Io. try out.writeAll(scratch.buffered()); } +/// A file opened in a pane (dev/Lsp.zig open): the server of its language +/// started as its first query would start it (`ensure`, which says +/// `starting` and `ready` on the message row) and told of the file +/// (didOpen), so that query finds both done. A server not installed is +/// disabled without a word: the file's first query says it is not found, +/// not every file opened. Nothing in the query log: none was asked. +/// PARDES_LSP_OPEN=off starts none (the process harnesses' default), and a +/// test binary starts none unless it says `on` (the behaviour harness's +/// language sessions, their servers the mock): no test starts a server of +/// this machine's by opening a file. +pub fn open(io: std.Io, arena: std.mem.Allocator, req: lsp.Req) Err!void { + const said = if (libc.getenv("PARDES_LSP_OPEN")) |v| std.mem.span(v) else ""; + if (std.mem.eql(u8, said, "off") or (builtin.is_test and !std.mem.eql(u8, said, "on"))) return; + const si = specFor(req.path) orelse return; + const c = &conns[si]; + try c.mu.lock(io); + defer c.mu.unlock(io); + if (c.state == .disabled) return; + var exe_buf: [4096:0]u8 = undefined; + if (!c.alive() and binOf(&specs[si], &exe_buf) == null) { + c.state = .disabled; + return; + } + var tr: Trace = .{}; + _ = try ensure(c, io, si, arena, req, &tr); + var uri: std.ArrayList(u8) = .empty; + try uriOf(&uri, arena, req.path); + try syncDoc(c, io, si, arena, uri.items, req.source); +} + fn traceOut(tr: *const Trace, req: lsp.Req, out: *std.Io.Writer, rows: usize, us: u64) std.Io.Writer.Error!void { if (req.kind != .explain) return; try out.print("lsp explain — the {s} query at byte {d} of {s}\n\n", .{ @@ -3162,11 +3194,53 @@ test "a language with no server to start says so, not that it did not answer" { try std.testing.expectEqualStrings("@none language server for .py not found\n", out.buffered()); } +test "a file opened whose server is not installed disables it without a word, at every file's open; a query of it then says it is not found" { + const host_io = @import("../host_io.zig"); + const io = std.testing.io; + const si = specFor("/x/a.py") orelse return; + const c = &conns[si]; + const was_state = c.state; + defer c.state = was_state; + // A test binary's files start servers only where it says so. + const names = [_][*:0]const u8{ "PARDES_LSP_PY", "PARDES_LSP_OPEN" }; + var was_buf: [names.len][4096:0]u8 = undefined; + var was: [names.len]?[:0]const u8 = undefined; + for (names, &was, &was_buf) |name, *w, *b| w.* = if (libc.getenv(name)) |v| try std.fmt.bufPrintSentinel(b, "{s}", .{std.mem.span(v)}, 0) else null; + defer for (names, was) |name, w| { + _ = if (w) |v| host_io.Shell.setenv(name, v.ptr, 1) else host_io.testUnsetenv(name); + }; + _ = host_io.Shell.setenv("PARDES_LSP_PY", "/nonexistent/pyright-langserver", 1); + _ = host_io.Shell.setenv("PARDES_LSP_OPEN", "on", 1); + const Said = struct { + var n: usize = 0; + fn post(_: ?*anyopaque, _: []const u8) void { + n += 1; + } + }; + Said.n = 0; + // A word said before is never said twice in a row (`post`): forgotten. + sink_last_len[si] = 0; + setStatusSink(null, Said.post); + defer setStatusSink(null, null); + c.state = .off; + var arena: std.heap.ArenaAllocator = .init(std.testing.allocator); + defer arena.deinit(); + for ([_][]const u8{ "/x/a.py", "/x/b.py" }) |path| { + try open(io, arena.allocator(), .{ .kind = .definition, .path = path, .source = "", .offset = 0, .root = "/x" }); + try std.testing.expectEqual(State.disabled, c.state); + } + try std.testing.expectEqual(@as(usize, 0), Said.n); + var buf: [256]u8 = undefined; + var out: std.Io.Writer = .fixed(&buf); + try query(io, arena.allocator(), .{ .kind = .hover, .path = "/x/a.py", .source = "", .offset = 0 }, &out); + try std.testing.expectEqualStrings("@none language server for .py not found\n", out.buffered()); +} + test "Lspinfo before any query says a server is not started yet, not off" { var out: std.Io.Writer.Allocating = .init(std.testing.allocator); defer out.deinit(); try status(std.testing.io, "/x/notes.txt", &out.writer); - try std.testing.expect(std.mem.indexOf(u8, out.written(), "not started yet (its first query starts it)") != null); + try std.testing.expect(std.mem.indexOf(u8, out.written(), "not started yet (a file of its opened starts it)") != null); } test "a server that failed a moment ago is said to have failed, and when it is tried again, not to be missing" { |
