summaryrefslogtreecommitdiff
path: root/dev/tui
diff options
context:
space:
mode:
authorGabriel Schneider <[email protected]>2026-11-19 12:59:58 -0300
committerGabriel Schneider <[email protected]>2026-11-19 13:00:11 -0300
commit280a2954152d3206f057055b0d59ca0ddc70485a (patch)
treee5ddf11d73b346d9162658b21b9c23ddec24670d /dev/tui
parentea833164aa83d02119301a204b91ff9a8eeff27a (diff)
parent60e956c332bc0aca49ea92e59fa1d1ec3ab418c2 (diff)
downloadpardes-280a2954152d3206f057055b0d59ca0ddc70485a.tar.gz
pardes-280a2954152d3206f057055b0d59ca0ddc70485a.zip
integration: the one session loop with the entries written once: pardes-runners-c1 (10c's first part: runtime/session.zig, which the terminal's and the window's sessions run) and pardes-entries (the tape's entries made by taped(inner), on the client loop and the handle's steps 0 to 2); two conflicts, one in each frontend, where the handle's step 2 had changed one argument of the dump's read inside the session function that the session loop replaced: resolved to the session loop's side, the argument (the tape a host half passes, &tape.none) carried to where the read went (runtime/session.zig); gates in the coordinator's ledger
Co-Authored-By: Claude Fable 5.2 <[email protected]> Claude-Session: https://claude.ai/code/session_016Y9MNBECMWcJD5pdS4SR6T
Diffstat (limited to 'dev/tui')
-rw-r--r--dev/tui/tui.zig268
1 files changed, 131 insertions, 137 deletions
diff --git a/dev/tui/tui.zig b/dev/tui/tui.zig
index 5a42ae5e..70ea3295 100644
--- a/dev/tui/tui.zig
+++ b/dev/tui/tui.zig
@@ -17,6 +17,7 @@ const selection_pipe = @import("core").selection_pipe;
const ninep_io = @import("runtime").ninep_io;
const daemon = @import("runtime").daemon;
const client_loop = @import("runtime").client;
+const session_loop = @import("runtime").session;
const host_io = @import("../host_io.zig");
const upgrade = @import("runtime").upgrade;
const replica = @import("core").replica;
@@ -617,7 +618,8 @@ pub fn run(init: std.process.Init, opts: pardes.Options, term: Term) !void {
defer paste_buf.deinit();
var loop: Loop = .init(io, &screen.tty, &screen.vx);
- try localSession(init, opts, &screen, &loop, &paste_buf);
+ var own: Own = .{ .screen = &screen, .loop = &loop, .paste_buf = &paste_buf };
+ try session_loop.Loop(Own).run(init, opts, &own);
}
/// A replica client's terminal (`runClient`): the frontend of the client
@@ -774,6 +776,8 @@ pub fn runClient(init: std.process.Init, opts: pardes.Options, path: []const u8,
var tty: Tty = undefined;
var c = try Client.init(&tty, &start);
defer c.link.deinit(gpa);
+ // A client has no clients of its own.
+ var no_peers: replica.Peers = .{};
var sh: Shell = .{
.io = io,
.gpa = gpa,
@@ -788,6 +792,7 @@ pub fn runClient(init: std.process.Init, opts: pardes.Options, path: []const u8,
.own = start.joined.view,
.screen = &screen,
.client = &c,
+ .peers = &no_peers,
};
tty = .{ .sh = &sh, .screen = &screen, .loop = &loop, .joins = &start };
@@ -935,72 +940,61 @@ const Killed = struct {
}
};
-fn localSession(
- init: std.process.Init,
- opts: pardes.Options,
+/// The session's own terminal (`run`): the frontend of the session loop
+/// (runtime/session.zig), its devices the `Shell`'s, which draws it.
+const Own = struct {
screen: *Screen,
loop: *Loop,
paste_buf: *std.Io.Writer.Allocating,
-) !void {
- const io = init.io;
- const gpa = init.gpa;
-
- const allocs = pardes.memory.init(gpa);
- defer pardes.memory.deinit();
- var options = opts;
- options.image_allocator = allocs.image;
- options.tree_sitter_allocator = allocs.tree_sitter;
- options.frame_allocator = allocs.frame;
- // Its frame timings, the terminal's to write as it draws (Pardes.frames).
- var frames: pardes.FrameStats = .{};
- options.frames = &frames;
-
- host_io.startWorkers(allocs);
- defer host_io.stopWorkers();
- options.addons = pardes.Addon.scan();
-
- var core = if (upgrade.resumed) |*rec|
- try upgrade.load(gpa, allocs.pardes, options, rec)
- else if (options.load_path) |lp| blk: {
- const bytes = try filesystem.readFile(&pardes.tape.none, gpa, lp);
- defer gpa.free(bytes);
- break :blk try pardes.dump.initFromDump(allocs.pardes, options, bytes);
- } else try pardes.Pardes.init(allocs.pardes, options);
- defer core.deinit();
+ frame_arena: std.heap.ArenaAllocator = undefined,
+ sh: Shell = undefined,
- var prompt_rcs = host_io.Shell.prepare();
- defer prompt_rcs.deinit();
+ const Session = session_loop.Loop(Own);
- var frame_arena: std.heap.ArenaAllocator = .init(allocs.frame);
- defer frame_arena.deinit();
+ pub fn options(_: *Own, _: *pardes.Options) void {}
- var fs = ninep_io.start(io, gpa, core);
- defer if (fs) |f| f.deinit(gpa);
+ pub fn open(o: *Own, l: *Session) !pardes.Host {
+ o.frame_arena = .init(l.allocs.frame);
+ o.sh = .{
+ .io = l.io,
+ .gpa = l.gpa,
+ .lsp_gpa = l.allocs.lsp,
+ .core = l.core,
+ .frames = &l.frames,
+ .prompt_rcs = &l.prompt_rcs,
+ .loop = o.loop,
+ .frame = &o.frame_arena,
+ .paste_buf = o.paste_buf,
+ .inotify_fd = file_watch.init(false),
+ .fs = l.fs,
+ .test_clock = host_io.testClock(),
+ .peers = &l.peers,
+ };
+ const sh = &o.sh;
+ sh.screen = o.screen;
+ 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;
+ pardes.Kind.drawer = .{ .ctx = sh, .go = Shell.draw };
+ // An upgraded session's terminals, its own again.
+ if (comptime !builtin.is_test) if (upgrade.resumed) |*rec| sh.resumeTerminals(rec);
+ // Nobody to see it, and nobody to size it: `size` sets it (ctl.zig).
+ sh.setAttended(!sh.detached());
+ return host;
+ }
- var sh: Shell = .{
- .io = io,
- .gpa = gpa,
- .lsp_gpa = allocs.lsp,
- .core = core,
- .frames = &frames,
- .prompt_rcs = &prompt_rcs,
- .loop = loop,
- .frame = &frame_arena,
- .paste_buf = paste_buf,
- .inotify_fd = file_watch.init(false),
- .fs = fs,
- .test_clock = host_io.testClock(),
- };
- sh.screen = screen;
- sh.peers = .{ .gpa = gpa, .io = io, .name = daemon.name(), .wake_ctx = loop, .wake = wakeForReplica };
- defer sh.peers.deinit();
- // Before the core goes: each client sees its link end.
- defer sh.peers.dropAll(sh.core);
- pardes.lsp.setStatusSink(&sh, lspStatusSink);
- host_io.setExitWake(loop, wakeForExit);
- defer screen.glide.deinit(gpa);
- defer screen.art.deinit(gpa);
- defer {
+ pub fn close(o: *Own, l: *Session) void {
+ const io = l.io;
+ const gpa = l.gpa;
+ const sh = &o.sh;
+ const loop = o.loop;
+ sh.fs = null;
+ unlinkKittyShm();
+ stopInput(io, &o.screen.thread);
+ pardes.Kind.drawer = null;
host_io.setExitWake(null, null);
pardes.lsp.setStatusSink(null, null);
for (&sh.ptys) |*slot| if (slot.*) |*pt| {
@@ -1016,7 +1010,7 @@ fn localSession(
sh.pipe_tasks.cancelAll(io);
sh.job_tasks.cancelAll(io);
// Its worker may be waiting on the loop's queue for room.
- core.stream.cancelTask();
+ sh.core.stream.cancelTask();
file_watch.stop(sh.inotify_fd);
if (sh.watch_task) |*t| {
t.cancel(io);
@@ -1032,8 +1026,8 @@ fn localSession(
gpa.free(pr.bytes);
},
.paste => |b| gpa.free(@constCast(b)),
- .lsp_done => |d| if (d.rows) |rows| allocs.lsp.free(rows),
- .lsp_status => |text| allocs.lsp.free(text),
+ .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);
@@ -1047,87 +1041,82 @@ fn localSession(
.adopted => |a| _ = libc.close(a.fd),
else => {},
};
+ o.screen.art.deinit(gpa);
+ o.screen.glide.deinit(gpa);
+ o.frame_arena.deinit();
}
- const host = sh.host();
- core.host = host;
- pardes.Kind.drawer = .{ .ctx = &sh, .go = Shell.draw };
- defer pardes.Kind.drawer = null;
- // An upgraded session's terminals, its own again.
- if (comptime !builtin.is_test) if (upgrade.resumed) |*rec| sh.resumeTerminals(rec);
- // Nobody to see it, and nobody to size it: `size` sets it (ctl.zig).
- sh.setAttended(!sh.detached());
- while (core.nextEffect()) |effect| core.perform(effect);
- // ...and its shells, once a fresh session's own has a directory to
- // adopt them in.
- if (upgrade.resumed) |*rec| {
- sh.resumePtys(rec);
- while (core.nextEffect()) |effect| core.perform(effect);
+ pub fn resumePtys(o: *Own, rec: *const upgrade.Record) void {
+ o.sh.resumePtys(rec);
}
- if (screen.on) try startInput(loop, screen);
- defer stopInput(io, &screen.thread);
- (try std.Thread.spawn(.{}, winchWatch, .{loop})).detach();
- (try std.Thread.spawn(.{}, wakeWatch, .{ io, loop })).detach();
- if (fs) |f| try f.watch(loop, wakeFs);
- if (screen.on) try screen.vx.queryTerminalSend(screen.tty.writer());
- // Detached from the start, nothing else may wake the first wait.
- if (!screen.on) daemon.watch(loop, deliverNote);
+ pub fn start(o: *Own, l: *Session) !void {
+ const io = l.io;
+ const sh = &o.sh;
+ const screen = o.screen;
+ const loop = o.loop;
+ if (screen.on) try startInput(loop, screen);
+ (try std.Thread.spawn(.{}, winchWatch, .{loop})).detach();
+ (try std.Thread.spawn(.{}, wakeWatch, .{ io, loop })).detach();
+ if (l.fs) |f| try f.watch(loop, wakeFs);
+ if (screen.on) try screen.vx.queryTerminalSend(screen.tty.writer());
+ // Detached from the start, nothing else may wake the first wait.
+ if (!screen.on) l.watch();
- sh.threads_ok = true;
- for (&sh.ptys, 0..) |*slot, id| if (slot.*) |*pt| {
- pt.reader = try io.concurrent(readPty, .{ io, gpa, pt.file, id, sh.gens[id], loop });
- };
- if (sh.inotify_fd >= 0) sh.watch_task = io.concurrent(watchFiles, .{ io, sh.inotify_fd, loop }) catch null;
+ sh.threads_ok = true;
+ for (&sh.ptys, 0..) |*slot, id| if (slot.*) |*pt| {
+ pt.reader = try io.concurrent(readPty, .{ io, l.gpa, pt.file, id, sh.gens[id], loop });
+ };
+ if (sh.inotify_fd >= 0) sh.watch_task = io.concurrent(watchFiles, .{ io, sh.inotify_fd, loop }) catch null;
+ }
- defer unlinkKittyShm();
- while (!core.quit) {
- pardes.turn.bump(&pardes.turn.restores);
- try core.pump(host);
- // Its unit to each replica client, and theirs taken in.
- sh.peers.boundary(core);
- // Detached, the unit is its clients' frame: its output is paced to
- // it as a terminal's frame paces it (waitInput), a unit a frame, not
- // a unit and a delta for every read of a flood.
- if (sh.peers.recording and sh.detached()) sh.drawn_ns = @intCast(std.Io.Clock.awake.now(io).nanoseconds);
- upgrade.locate();
- // Asked in the step (the word), done between steps: the session whole.
- if (sh.upgrade_asked) |pane| sh.upgradeNow(pane);
- // A terminal (`pardes --attach`) or an adopted shell, handed on by
- // the daemon's thread to this loop; started after the first frame,
- // off a startup's way.
- daemon.watch(loop, deliverNote);
- if (core.takeRestore()) |rp| blk: {
- const nc = host_io.restoreCore(gpa, core, rp, &sh.peers, sh.inotify_fd, &sh.watches) orelse break :blk;
- if (sh.lsp_task) |*task| {
- task.future.cancel(io);
- sh.lsp_task = null;
- }
- sh.pipe_tasks.cancelAll(io);
- for (&sh.gens) |*generation| generation.* +%= 1;
- for (0..pardes.MAX_PANES) |pane| Shell.closePty(&sh, @intCast(pane));
- // One view, the terminal's (host_io.restoreCore).
- const live = screen;
- stopInput(io, &live.thread);
- sh.own = 0;
- if (live.on) startInput(loop, live) catch {};
- if (live.on) clearNativeImages(&live.kitty, &live.vx, &live.tty);
- nc.view().frontend.native_images = live.on and live.vx.caps.kitty_graphics;
- nc.unattended = !live.on;
- sh.peers.detached = !live.on;
- if (fs) |f| f.reset(nc);
- core.deinit();
- core = nc;
- sh.core = nc;
- if (live.on) core.update(.{ .resize = terminalSize(live.vx.screen.width, live.vx.screen.height, live.vx.screen.width_pix, live.vx.screen.height_pix) });
+ /// Detached, the unit is its clients' frame: its output is paced to it
+ /// as a terminal's frame paces it (waitInput), a unit a frame, not a
+ /// unit and a delta for every read of a flood.
+ pub fn sent(o: *Own) void {
+ const sh = &o.sh;
+ if (sh.peers.recording and sh.detached()) sh.drawn_ns = @intCast(std.Io.Clock.awake.now(sh.io).nanoseconds);
+ }
+
+ pub fn upgradeAsked(o: *Own) void {
+ if (o.sh.upgrade_asked) |pane| o.sh.upgradeNow(pane);
+ }
+
+ pub fn note(o: *Own, n: daemon.Note) void {
+ deliverNote(o.loop, n);
+ }
+
+ pub fn watched(o: *Own) session_loop.Watched {
+ return .{ .fd = o.sh.inotify_fd, .table = &o.sh.watches };
+ }
+
+ 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);
+ for (&sh.gens) |*generation| generation.* +%= 1;
+ for (0..pardes.MAX_PANES) |pane| Shell.closePty(sh, @intCast(pane));
+ // One view, the terminal's (host_io.restoreCore).
+ const live = o.screen;
+ stopInput(io, &live.thread);
+ sh.own = 0;
+ if (live.on) startInput(o.loop, live) catch {};
+ if (live.on) clearNativeImages(&live.kitty, &live.vx, &live.tty);
+ nc.view().frontend.native_images = live.on and live.vx.caps.kitty_graphics;
+ nc.unattended = !live.on;
+ sh.peers.detached = !live.on;
}
- if (fs) |f| {
- f.deinit(gpa);
- fs = null;
- sh.fs = null;
+
+ pub fn restored(o: *Own, l: *Session) void {
+ o.sh.core = l.core;
+ const live = o.screen;
+ if (live.on) l.core.update(.{ .resize = terminalSize(live.vx.screen.width, live.vx.screen.height, live.vx.screen.width_pix, live.vx.screen.height_pix) });
}
-}
+};
fn clearNativeImages(
kitty_handles: *std.AutoHashMap(u64, vaxis.Image),
@@ -1220,8 +1209,9 @@ const Shell = struct {
upgrade_asked: ?u8 = null,
/// Its pty readers stopped for it: their end of file is not a shell's.
upgrading: bool = false,
- /// The session's replica clients (core/replica.zig), on sockets.
- peers: replica.Peers = .{},
+ /// The session's replica clients (core/replica.zig), on sockets: the
+ /// session loop's (runtime/session.zig); a client's, none.
+ peers: *replica.Peers,
/// This wait took only a replica's post (its input, its join): the
/// pump after it reads no new time, so it moves nothing the replicas'
/// speculations stand on (a replica takes such a unit as is).
@@ -2999,6 +2989,7 @@ test "TTY queued results reject old PTY generations and LSP request IDs" {
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,
@@ -3010,6 +3001,7 @@ test "TTY queued results reject old PTY generations and LSP request IDs" {
.frame = undefined,
.paste_buf = undefined,
.inotify_fd = -1,
+ .peers = &no_peers,
};
shell.gens[0] = 2;
_ = shell.apply(.{ .pty_read = .{ .id = 0, .gen = 1, .bytes = try gpa.dupe(u8, "old session\n") } });
@@ -3045,6 +3037,7 @@ test "TTY worker setup failures finish matching LSP and pipe requests" {
var failing = std.testing.FailingAllocator.init(gpa, .{
.fail_index = if (failure == 0) 0 else std.math.maxInt(usize),
});
+ var no_peers: replica.Peers = .{};
var shell: Shell = .{
.io = failing_io,
.gpa = failing.allocator(),
@@ -3057,6 +3050,7 @@ test "TTY worker setup failures finish matching LSP and pipe requests" {
.paste_buf = undefined,
.inotify_fd = -1,
.threads_ok = true,
+ .peers = &no_peers,
};
core.lspRequest(core.view().active, .status, "");
const req: host_io.Lsp.Request = .{