From 3eb6c09bc97c2e60a17e1663108f4b22e3d1308c Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Fri, 20 Nov 2026 01:45:46 -0300 Subject: dev, runtime: a session's file watches once (file_watch.Watches, a working name), the session loop's beside its ptys and queue: the kernel's descriptor, the table (each pane's mark and the theme's slot), the reader that posts `files_changed` to the one queue (the Inbox) and the Host's three file calls (Watches.Calls fills both frontends' write_file, watch_file, watch_theme); the terminal's reader (an Io task posting to vaxis's queue, its files_changed message) and the window's (a thread with a stop pipe, polling a non-blocking descriptor) are deleted for one Io task on a blocking descriptor ended by its cancel, as the ptys' readers are; a Restore lets go of the loop's table, so the frontend call `watched` is gone (ten calls to nine); what a user sees: nothing (the queue keeps one change flag however many reads posted it; a reader that cannot start leaves the session running unwatched, as the terminal's did, where the window's start failed); tui core-test 857/1007 (150 skipped; +1, the device's test), shell-test 1047/1050, term 17/17, pane 183/184, kitty 45/45, behaviour-test and --replay 275/275, process-test 0 of 144 failed (ttytaken once in the run, passed on its retry), soaks --clients 5 and --bytes --clients 5 diverged 0, unequal 0, quiet checks 597/0 673/0 779/0 and 920/0 723/0 755/0, unlisted 0, corpus scene 22 of 22, no leak; gui unit-test 1643/1647 with only the perf check's BaselineHarnessMismatch; daemon.py plain, --far, --games, --window-clients, --upgrade both ways pass; the macOS library (Linux, aarch64-macos) and FreeBSD build (load 9-18) Co-Authored-By: Claude Opus Claude-Session: https://claude.ai/code/session_016Y9MNBECMWcJD5pdS4SR6T --- runtime/session.zig | 30 +++++++++++++++--------------- 1 file changed, 15 insertions(+), 15 deletions(-) (limited to 'runtime/session.zig') diff --git a/runtime/session.zig b/runtime/session.zig index 3942aa83..6f8ab4a3 100644 --- a/runtime/session.zig +++ b/runtime/session.zig @@ -3,11 +3,12 @@ //! the session's allocators and workers, its core (an upgrade's, a dump's //! or a new one), the prompt files its shells start with, its 9P listener //! its replica clients (`Peers`), its queue (the workers' and readers' -//! posts, `Inbox`) and its ptys (`Ptys`): a step, its unit to the clients -//! and theirs taken in, an Upgrade or a Restore between steps; and the -//! order its parts end in. Its other devices (the files watched, the LSP's -//! and the pipes' and addons' jobs, the session's own input) are still its -//! frontend's (each `Shell`). What a frontend does is: +//! posts, `Inbox`), its ptys (`Ptys`) and the files it watches +//! (`file_watch.Watches`): a step, its unit to the clients and theirs +//! taken in, an Upgrade or a Restore between steps; and the order its +//! parts end in. Its other devices (the LSP's and the pipes' and addons' +//! jobs, the session's own input) are still its frontend's (each `Shell`). +//! What a frontend does is: //! //! - `options(o)`: before the core: what it gives the core it starts (a //! window's size, for a dump's layout). @@ -17,15 +18,14 @@ //! core's first effects (an upgraded terminal's panes its own again, //! whether anyone looks). //! - `close(l)`: its devices stopped, after the clients are let go and -//! before the ptys, the queue and the core go. -//! - `start(l)`: after the first effects, the ptys read: its input, its -//! readers, the -//! listener's wake, a detached session's first wait (`l.watch`). +//! before the watches, the ptys, the queue and the core go. +//! - `start(l)`: after the first effects, the ptys and the watches +//! read: its input, its readers, the listener's wake, a detached +//! session's first wait (`l.watch`). //! - `sent()`: after each step's unit went to the clients. //! - `upgradeAsked()`: an Upgrade asked in the step, done (it execs). //! - `note(n)`: what the daemon says (a terminal, an adopted shell, a //! client's connection), onto its queue. -//! - `watched()`: the files it watches (a Restore stops watching them). //! - `restoring(nc)`, `restored(l)`: a Restore's new core, before it //! replaces the old one (its devices let go, what it is told of the //! frontend) and after (its own view of it again). @@ -39,9 +39,6 @@ const daemon = @import("daemon.zig"); const upgrade = @import("upgrade.zig"); const ninep_io = @import("9p_io.zig"); -/// The files a frontend watches (host_io.restoreCore lets them go). -pub const Watched = struct { fd: c_int, table: *file_watch.Table }; - pub fn Loop(comptime Frontend: type) type { return struct { const L = @This(); @@ -58,6 +55,7 @@ pub fn Loop(comptime Frontend: type) type { /// to take in its wait. inbox: host_io.Inbox = undefined, ptys: Ptys = undefined, + watches: file_watch.Watches = undefined, fs: ?*ninep_io.Listener = null, peers: replica.Peers = .{}, host: pardes.Host = undefined, @@ -93,6 +91,8 @@ pub fn Loop(comptime Frontend: type) type { defer l.inbox.deinit(); l.ptys = .{ .queue = &l.inbox, .prompt_rcs = &l.prompt_rcs }; defer l.ptys.deinit(); + l.watches = .open(l.io, l.gpa, &l.inbox); + defer l.watches.deinit(); l.host = try fe.open(&l); defer fe.close(&l); @@ -107,6 +107,7 @@ pub fn Loop(comptime Frontend: type) type { l.effects(); } l.ptys.start(l.core); + l.watches.start(); try fe.start(&l); while (!l.core.quit) { @@ -154,8 +155,7 @@ pub fn Loop(comptime Frontend: type) type { /// let go (host_io.restoreCore); its shells and the frontend's /// devices with it. fn restore(l: *L, path: []const u8) void { - const w = l.fe.watched(); - const nc = host_io.restoreCore(l.gpa, l.core, path, &l.peers, w.fd, w.table) orelse return; + const nc = host_io.restoreCore(l.gpa, l.core, path, &l.peers, l.watches.fd, &l.watches.table) orelse return; l.fe.restoring(nc); l.ptys.stop(); nc.host = l.host; -- cgit v1.3 From 680cfe1fa5a15be3770d310153258ba9ded13dd8 Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Fri, 20 Nov 2026 02:35:56 -0300 Subject: dev, runtime: the language servers' client once, through the session's one queue: both frontends start a query with Inbox.startLsp (its task and its id in the Inbox, an older answer turned away at the push and an older one still queued discarded) and land an answer or a status line with Inbox.landLsp; the session loop sets the language client's status sink on its queue from before the first effects and unsets it before the queue goes, a Restore turns the old core's answer away (discardCompletions, the window's way) and the queue's end cancels the query in flight; the terminal's own copy is deleted (its lsp_task, deliverLspRows posting lsp_done to vaxis's queue, a post that BLOCKED the language worker while that queue was full, lspStatusSink, the lsp_done and lsp_status messages and arms, the cancels in close and restoring) and with it its test of a stale answer at apply (the Inbox's rejection is pinned by gui.zig's worker-setup test); what a user sees: a window now shows an LSP status said during the session's first effects (its sink was set only after them), nothing else; the loop's frontend calls stay nine (close and restoring shrank); tui core-test 857/1007 (150 skipped), shell-test 1046/1049 (-1, the deleted test), term 17/17, pane 183/184, kitty 45/45, behaviour-test and --replay 275/275, process-test 0 of 144 failed (hscroll once in the run, passed on its retry), soaks --clients 5 and --bytes --clients 5 diverged 0, unequal 0, quiet checks 651/0 673/0 779/0 and 920/0 723/0 755/0, unlisted 0, corpus scene 22 of 22, no leak; gui unit-test 1643/1647 with only the perf check's BaselineHarnessMismatch; daemon.py plain, --far, --games, --window-clients, --upgrade both ways pass (load 11-16) Co-Authored-By: Claude Opus Claude-Session: https://claude.ai/code/session_016Y9MNBECMWcJD5pdS4SR6T --- dev/Inbox.zig | 20 ++++++++++++ dev/gui/gui.zig | 14 +-------- dev/tui/tui.zig | 88 ++++++----------------------------------------------- runtime/session.zig | 8 ++++- 4 files changed, 37 insertions(+), 93 deletions(-) (limited to 'runtime/session.zig') diff --git a/dev/Inbox.zig b/dev/Inbox.zig index ed3a9835..1dd30adf 100644 --- a/dev/Inbox.zig +++ b/dev/Inbox.zig @@ -264,6 +264,9 @@ pub fn close(q: *Inbox) void { } pub fn deinit(q: *Inbox) void { + // The query in flight ends first: what it still delivers is dropped. + if (q.lsp_task) |*t| t.future.cancel(q.io); + q.lsp_task = null; q.close(); std.debug.assert(q.waiting == 0); } @@ -310,6 +313,23 @@ pub fn startLsp(q: *Inbox, core: *pardes.Pardes, e: Lsp.Request) void { Lsp.start(q.io, q.lsp_allocator, core, &q.lsp_task, e, q, pushLspRows); } +/// A language server's answer or status line taken off the queue, to the +/// core (whichever frontend took it). +pub fn landLsp(q: *Inbox, core: *pardes.Pardes, m: Msg) void { + switch (m) { + .lsp => |l| { + core.update(.{ .lsp_resp = .{ .id = l.id, .rows = l.rows } }); + if (l.rows) |rows| q.lsp_allocator.free(rows); + }, + .lsp_status => |text| { + var mbuf: [256]u8 = undefined; + core.setStatus(.at(core.view().active), pardes.Messages.Message.stamp(&mbuf, "lsp", text)); + q.lsp_allocator.free(text); + }, + else => unreachable, + } +} + fn pushLspRows(ctx: ?*anyopaque, id: u32, rows: ?[]u8) void { const q: *Inbox = @ptrCast(@alignCast(ctx orelse return)); q.push(.{ .lsp = .{ .id = id, .rows = rows } }); diff --git a/dev/gui/gui.zig b/dev/gui/gui.zig index 59e0516a..df3d2f2b 100644 --- a/dev/gui/gui.zig +++ b/dev/gui/gui.zig @@ -1047,11 +1047,9 @@ const Own = struct { const io = l.io; o.shell.fs = null; host_io.setExitWake(null, null); - pardes.lsp.setStatusSink(null, null); pardes.Kind.drawer = null; o.shell.job_tasks.cancelAll(io); o.pipe_tasks.cancelAll(io); - if (l.inbox.lsp_task) |*task| task.future.cancel(io); // Its worker may be waiting on the queue for room. l.core.stream.cancelTask(); o.g.hit_core = null; @@ -1066,7 +1064,6 @@ const Own = struct { if (o.test_mode) setStdinRaw() catch {}; o.shell.threads_ok = true; - pardes.lsp.setStatusSink(&l.inbox, Queue.lspStatus); host_io.setExitWake(&l.inbox, Queue.wakeForExit); if (o.detached) { o.shell.closeTheWindow(); @@ -1087,7 +1084,6 @@ const Own = struct { pub fn restoring(o: *Own, nc: *pardes.Pardes) void { o.pipe_tasks.cancelAll(o.shell.io); - o.shell.queue.discardCompletions(); // One view, the window's (host_io.restoreCore). o.shell.gview = 0; o.g.cells.clearImages(); @@ -2601,15 +2597,7 @@ const Shell = struct { for (msgs.slice()) |m| switch (m) { .output => |o| s.ptys.output(s.core, o), .eof => |e| s.ptys.eof(s.core, e), - .lsp => |l| { - s.core.update(.{ .lsp_resp = .{ .id = l.id, .rows = l.rows } }); - if (l.rows) |rows| s.lsp_allocator.free(rows); - }, - .lsp_status => |text| { - var mbuf: [256]u8 = undefined; - s.core.setStatus(.at(s.core.view().active), message.stamp(&mbuf, "lsp", text)); - s.lsp_allocator.free(text); - }, + .lsp, .lsp_status => s.queue.landLsp(s.core, m), .pipe => |response_value| { var response = response_value; s.core.update(.{ .pipe_resp = .{ diff --git a/dev/tui/tui.zig b/dev/tui/tui.zig index b20c15a0..1c59a8be 100644 --- a/dev/tui/tui.zig +++ b/dev/tui/tui.zig @@ -123,8 +123,6 @@ const Msg = union(enum) { paste: []const u8, paste_start, paste_end, - lsp_done: struct { id: u32, rows: ?[]u8 }, - lsp_status: []u8, pipe_done: selection_pipe.Response, /// A streaming search's rows (pardes.Stream). search_rows: pardes.Stream.Batch, @@ -969,7 +967,6 @@ const Own = struct { l.inbox.wake_ctx = sh; l.peers.wake_ctx = o.loop; l.peers.wake = wakeForReplica; - pardes.lsp.setStatusSink(sh, lspStatusSink); host_io.setExitWake(o.loop, wakeForExit); const host = sh.host(); l.core.host = host; @@ -991,19 +988,12 @@ const Own = struct { stopInput(io, &o.screen.thread); pardes.Kind.drawer = null; host_io.setExitWake(null, null); - pardes.lsp.setStatusSink(null, null); - if (sh.lsp_task) |*t| { - t.future.cancel(io); - sh.lsp_task = null; - } sh.pipe_tasks.cancelAll(io); sh.job_tasks.cancelAll(io); // Its worker may be waiting on the loop's queue for room. sh.core.stream.cancelTask(); while (loop.tryEvent() catch null) |ev| switch (ev) { .paste => |b| gpa.free(@constCast(b)), - .lsp_done => |d| if (d.rows) |rows| sh.lsp_gpa.free(rows), - .lsp_status => |text| sh.lsp_gpa.free(text), .pipe_done => |response_value| { var response = response_value; response.deinit(gpa); @@ -1057,10 +1047,6 @@ const Own = struct { pub fn restoring(o: *Own, nc: *pardes.Pardes) void { const sh = &o.sh; const io = sh.io; - if (sh.lsp_task) |*task| { - task.future.cancel(io); - sh.lsp_task = null; - } sh.pipe_tasks.cancelAll(io); // One view, the terminal's (host_io.restoreCore). const live = o.screen; @@ -1149,7 +1135,6 @@ const Shell = struct { batch: host_io.Inbox.Batch = .{}, batch_at: usize = 0, queue_woken: std.atomic.Value(bool) = .init(false), - lsp_task: ?host_io.Lsp.Task = null, pipe_tasks: PipeTasks = .{}, job_tasks: PipeTasks = .{}, threads_ok: bool = false, @@ -1710,19 +1695,6 @@ const Shell = struct { s.paste_buf.clearRetainingCapacity(); }, .fs_ready => {}, - .lsp_done => |d| { - core.update(.{ .lsp_resp = .{ .id = d.id, .rows = d.rows } }); - if (d.rows) |rows| s.lsp_gpa.free(rows); - if (s.lsp_task) |*t| if (t.id == d.id) { - t.future.await(s.io); - s.lsp_task = null; - }; - }, - .lsp_status => |text| { - var mbuf: [256]u8 = undefined; - core.setStatus(.at(core.view().active), message.stamp(&mbuf, "lsp", text)); - s.lsp_gpa.free(text); - }, .pipe_done => |response_value| { var response = response_value; core.update(.{ .pipe_resp = .{ @@ -1980,6 +1952,7 @@ const Shell = struct { }, .eof => |e| s.ptys.eof(s.core, e), .files_changed => s.check_files = true, + .lsp, .lsp_status => s.ptys.queue.landLsp(s.core, m), else => m.deinit(s.gpa, s.lsp_gpa), } } @@ -2024,16 +1997,9 @@ const Shell = struct { fn lsp(ctx: ?*anyopaque, req: host_io.Lsp.Request) void { const s = of(ctx); - if (!s.threads_ok) { - s.core.update(.{ .lsp_resp = .{ .id = req.id, .rows = null } }); - return s.core.reportError(req.pane, "lsp", error.WorkersUnavailable); - } - host_io.Lsp.start(s.io, s.lsp_gpa, s.core, &s.lsp_task, req, s, deliverLspRows); - } - - fn deliverLspRows(ctx: ?*anyopaque, id: u32, rows: ?[]u8) void { - const s = of(ctx); - s.loop.postEvent(.{ .lsp_done = .{ .id = id, .rows = rows } }) catch if (rows) |owned| s.lsp_gpa.free(owned); + if (s.threads_ok) return s.ptys.queue.startLsp(s.core, req); + s.core.update(.{ .lsp_resp = .{ .id = req.id, .rows = null } }); + s.core.reportError(req.pane, "lsp", error.WorkersUnavailable); } fn pipe(ctx: ?*anyopaque, id: u32) void { @@ -2089,13 +2055,6 @@ const Shell = struct { } }; -fn lspStatusSink(ctx: ?*anyopaque, text: []const u8) void { - const s: *Shell = @ptrCast(@alignCast(ctx orelse return)); - const copy = s.lsp_gpa.dupe(u8, text) catch return; - const posted = s.loop.tryPostEvent(.{ .lsp_status = copy }) catch false; - if (!posted) s.lsp_gpa.free(copy); -} - fn keepSignal(_: posix.SIG) callconv(.c) void {} /// A replica client's (`runClient`): a hangup is its end, whoever sent it. @@ -2773,38 +2732,6 @@ test "on a terminal with no kitty graphics a program's 64x64 image over 8x4 cell try std.testing.expectEqual(@as(usize, 32), painted); } -test "TTY queued results reject an old LSP request ID" { - const gpa = std.testing.allocator; - const core = try pardes.Pardes.init(gpa, .{ .tty_only = true, .cols = 40, .rows = 12 }); - defer core.deinit(); - var no_peers: replica.Peers = .{}; - var shell: Shell = .{ - .io = std.testing.io, - .gpa = gpa, - .lsp_gpa = gpa, - .core = core, - .frames = core.frames, - .loop = undefined, - .frame = undefined, - .paste_buf = undefined, - .watches = undefined, - .peers = &no_peers, - .ptys = undefined, - }; - core.lspRequest(0, .status, ""); - const old_id = core.lsp_ask.wait.?.id; - core.lspRequest(0, .status, ""); - const current_id = core.lsp_ask.wait.?.id; - shell.lsp_task = .{ .id = current_id, .future = .{ .any_future = null, .result = {} } }; - _ = shell.apply(.{ .lsp_done = .{ .id = old_id, .rows = try gpa.dupe(u8, "stale result\n") } }); - try std.testing.expect(shell.lsp_task != null); - try std.testing.expectEqual(current_id, shell.lsp_task.?.id); - try std.testing.expectEqual(current_id, core.lsp_ask.wait.?.id); - _ = shell.apply(.{ .lsp_done = .{ .id = current_id, .rows = try gpa.dupe(u8, "") } }); - try std.testing.expect(shell.lsp_task == null); - try std.testing.expect(core.lsp_ask.wait == null); -} - test "TTY worker setup failures finish matching LSP and pipe requests" { const gpa = std.testing.allocator; var failing_vtable = std.testing.io.vtable.*; @@ -2818,6 +2745,9 @@ test "TTY worker setup failures finish matching LSP and pipe requests" { .fail_index = if (failure == 0) 0 else std.math.maxInt(usize), }); var no_peers: replica.Peers = .{}; + var queue: host_io.Inbox = .{ .io = failing_io, .gpa = gpa, .lsp_allocator = failing.allocator() }; + defer queue.deinit(); + var ptys: host_io.Ptys = .{ .queue = &queue, .prompt_rcs = undefined }; var shell: Shell = .{ .io = failing_io, .gpa = failing.allocator(), @@ -2830,7 +2760,7 @@ test "TTY worker setup failures finish matching LSP and pipe requests" { .watches = undefined, .threads_ok = true, .peers = &no_peers, - .ptys = undefined, + .ptys = &ptys, }; core.lspRequest(core.view().active, .status, ""); const req: host_io.Lsp.Request = .{ @@ -2853,7 +2783,7 @@ test "TTY worker setup failures finish matching LSP and pipe requests" { Shell.pipe(&shell, pipe_id); try std.testing.expect(core.pipe.wait == null); try std.testing.expectEqualStrings("abc", pane.file.?.content); - try std.testing.expect(shell.lsp_task == null); + try std.testing.expect(queue.lsp_task == null); try std.testing.expectEqual(@as(usize, 0), shell.pipe_tasks.len); } } diff --git a/runtime/session.zig b/runtime/session.zig index 6f8ab4a3..c8b8dc25 100644 --- a/runtime/session.zig +++ b/runtime/session.zig @@ -7,7 +7,8 @@ //! (`file_watch.Watches`): a step, its unit to the clients and theirs //! taken in, an Upgrade or a Restore between steps; and the order its //! parts end in. Its other devices (the LSP's and the pipes' and addons' -//! jobs, the session's own input) are still its frontend's (each `Shell`). +//! jobs, the session's own input) are still its frontend's (each `Shell`); +//! the language servers' answers come through its queue (`Inbox.startLsp`). //! What a frontend does is: //! //! - `options(o)`: before the core: what it gives the core it starts (a @@ -89,6 +90,9 @@ pub fn Loop(comptime Frontend: type) type { l.peers = .{ .gpa = l.gpa, .io = l.io, .name = daemon.name() }; l.inbox = .{ .io = l.io, .gpa = l.gpa, .lsp_allocator = l.allocs.lsp }; defer l.inbox.deinit(); + // The language servers' status lines, from the first effects on. + pardes.lsp.setStatusSink(&l.inbox, host_io.Inbox.lspStatus); + defer pardes.lsp.setStatusSink(null, null); l.ptys = .{ .queue = &l.inbox, .prompt_rcs = &l.prompt_rcs }; defer l.ptys.deinit(); l.watches = .open(l.io, l.gpa, &l.inbox); @@ -157,6 +161,8 @@ pub fn Loop(comptime Frontend: type) type { fn restore(l: *L, path: []const u8) void { const nc = host_io.restoreCore(l.gpa, l.core, path, &l.peers, l.watches.fd, &l.watches.table) orelse return; l.fe.restoring(nc); + // An answer to the old core's query is turned away. + l.inbox.discardCompletions(); l.ptys.stop(); nc.host = l.host; if (l.fs) |f| f.reset(nc); -- cgit v1.3