summaryrefslogtreecommitdiff
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
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
-rw-r--r--dev/gui/gui.zig330
-rw-r--r--dev/tui/tui.zig268
-rw-r--r--runtime/root.zig2
-rw-r--r--runtime/session.zig155
4 files changed, 445 insertions, 310 deletions
diff --git a/dev/gui/gui.zig b/dev/gui/gui.zig
index 237b7b85..798368e3 100644
--- a/dev/gui/gui.zig
+++ b/dev/gui/gui.zig
@@ -31,6 +31,7 @@ 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 upgrade = @import("runtime").upgrade;
const replica = @import("core").replica;
const tracy = @import("core").tracy;
@@ -984,7 +985,8 @@ fn runNative(init: std.process.Init, opts_in: pardes.Options, detached: bool, di
if (!test_mode and !c.SDL_SetWindowHitTest(window, hitTest, &g))
log.warn("SDL_SetWindowHitTest: {s}", .{c.SDL_GetError()});
if (dial) |to| return clientSession(init, &g, opts_in, test_mode, to);
- try localSession(init, &g, opts_in, test_mode, detached);
+ var own: Own = .{ .g = &g, .test_mode = test_mode, .detached = detached };
+ try session_loop.Loop(Own).run(init, opts_in, &own);
}
/// Detach: the window closed (and with it its swapchain or renderer); the
@@ -1019,195 +1021,173 @@ fn reopenWindow(g: *Gui) bool {
return true;
}
-fn localSession(
- init: std.process.Init,
+/// The session's own window (`runNative`): the frontend of the session loop
+/// (runtime/session.zig), its devices the `Shell`'s, which draws it.
+const Own = struct {
g: *Gui,
- opts_in: pardes.Options,
test_mode: bool,
detached: bool,
-) !void {
- const io = init.io;
- const gpa = init.gpa;
- const allocs = pardes.memory.init(gpa);
- defer pardes.memory.deinit();
- host_io.startWorkers(allocs);
- defer host_io.stopWorkers();
- var opts = opts_in;
- opts.addons = pardes.Addon.scan();
- // Its frame timings, the window's to write as it draws (Pardes.frames).
- var frames: pardes.FrameStats = .{};
- opts.frames = &frames;
- opts.image_allocator = allocs.image;
- opts.tree_sitter_allocator = allocs.tree_sitter;
- opts.frame_allocator = allocs.frame;
- var pw: c_int = 0;
- var ph: c_int = 0;
- if (opts.load_path != null) {
- _ = c.SDL_GetWindowSizeInPixels(g.window, &pw, &ph);
- opts.cols = @intCast(@max(1, @divTrunc(@as(u32, @intCast(@max(pw, 1))), g.cell_w)));
- opts.rows = @intCast(@max(1, @divTrunc(@as(u32, @intCast(@max(ph, 1))), g.cell_h)));
+ ptys: [pardes.MAX_PANES]?Pty = @splat(null),
+ gens: [pardes.MAX_PANES]u32 = @splat(0),
+ queue: Queue = undefined,
+ pipe_tasks: PipeTasks = .{},
+ inotify_fd: c_int = -1,
+ watch_reader: ?std.Thread = null,
+ watch_stop: [2]c_int = .{ -1, -1 },
+ watches: file_watch.Table = @splat(null),
+ shell: Shell = undefined,
+
+ const Session = session_loop.Loop(Own);
+
+ /// A dump's layout is laid out at the window's size.
+ pub fn options(o: *Own, opts: *pardes.Options) void {
+ if (opts.load_path == null) return;
+ var pw: c_int = 0;
+ var ph: c_int = 0;
+ _ = c.SDL_GetWindowSizeInPixels(o.g.window, &pw, &ph);
+ opts.cols = @intCast(@max(1, @divTrunc(@as(u32, @intCast(@max(pw, 1))), o.g.cell_w)));
+ opts.rows = @intCast(@max(1, @divTrunc(@as(u32, @intCast(@max(ph, 1))), o.g.cell_h)));
}
- var core = if (upgrade.resumed) |*rec|
- try upgrade.load(gpa, allocs.pardes, opts, rec)
- else if (opts.load_path) |lp| blk: {
- const bytes = try @import("core").filesystem.readFile(&pardes.tape.none, gpa, lp);
- defer gpa.free(bytes);
- break :blk try pardes.dump.initFromDump(allocs.pardes, opts, bytes);
- } else try pardes.Pardes.init(allocs.pardes, opts);
- defer core.deinit();
- g.hit_core = core;
- defer g.hit_core = null;
- core.view().frontend.windowed = true;
- core.view().frontend.native_images = true;
- core.view().frontend.native_image_patches = true;
- observeGuiFont(g, core);
- syncTaglineFont(g, core);
- var prompt_rcs = host_io.Shell.prepare();
- defer prompt_rcs.deinit();
+ pub fn open(o: *Own, l: *Session) !pardes.Host {
+ const g = o.g;
+ const core = l.core;
+ g.hit_core = core;
+ core.view().frontend.windowed = true;
+ core.view().frontend.native_images = true;
+ core.view().frontend.native_image_patches = true;
+ observeGuiFont(g, core);
+ syncTaglineFont(g, core);
+ o.queue = .{
+ .io = l.io,
+ .gpa = l.gpa,
+ .lsp_allocator = l.allocs.lsp,
+ .wake_fn = sdlWake,
+ };
+ o.inotify_fd = file_watch.init(true);
+ o.shell = .{
+ .core = core,
+ .gui = g,
+ .io = l.io,
+ .gpa = l.gpa,
+ .lsp_allocator = l.allocs.lsp,
+ .prompt_rcs = &l.prompt_rcs,
+ .ptys = &o.ptys,
+ .gens = &o.gens,
+ .queue = &o.queue,
+ .pipe_tasks = &o.pipe_tasks,
+ .inotify_fd = o.inotify_fd,
+ .watches = &o.watches,
+ .fs = l.fs,
+ .test_mode = o.test_mode,
+ .test_clock = if (o.test_mode) host_io.testClock() else null,
+ .test_hz = if (std.c.getenv("PARDES_TEST_HZ")) |hz| @max(1, std.fmt.parseInt(u64, std.mem.span(hz), 10) catch 60) else 60,
+ .peers = &l.peers,
+ };
+ // A virtual clock never blinks: goldens and feel series hold still.
+ g.no_blink = o.shell.test_clock != null;
+ frame_probe = &l.frames;
+ frame_capture_dir = if (std.c.getenv("PARDES_FRAMES_CAPTURE")) |dir| std.mem.span(dir) else null;
+ const host = o.shell.host();
+ core.host = host;
+ pardes.Kind.drawer = .{ .ctx = &o.shell, .go = kindDraw };
+ l.peers.wake = sdlWake;
+ l.peers.detached = o.detached;
+ return host;
+ }
- var ptys: [pardes.MAX_PANES]?Pty = @splat(null);
- var gens: [pardes.MAX_PANES]u32 = @splat(0);
- var queue: Queue = .{
- .io = io,
- .gpa = gpa,
- .lsp_allocator = allocs.lsp,
- .wake_fn = sdlWake,
- };
- defer {
- if (queue.lsp_task) |*task| task.future.cancel(io);
+ pub fn close(o: *Own, l: *Session) void {
+ const io = l.io;
+ o.shell.fs = null;
+ host_io.setExitWake(null, null);
+ pardes.lsp.setStatusSink(null, null);
+ pardes.Kind.drawer = null;
+ o.shell.shutdownPtys();
+ o.shell.job_tasks.cancelAll(io);
+ if (o.inotify_fd >= 0) {
+ if (o.watch_reader) |thread| {
+ _ = host_io.writeFd(o.watch_stop[1], "x");
+ thread.join();
+ }
+ for (o.watch_stop) |fd| if (fd >= 0) {
+ _ = libc.close(fd);
+ };
+ file_watch.stop(o.inotify_fd);
+ _ = libc.close(o.inotify_fd);
+ o.inotify_fd = -1;
+ }
+ o.pipe_tasks.cancelAll(io);
+ if (o.queue.lsp_task) |*task| task.future.cancel(io);
// Its worker may be waiting on the queue for room.
- core.stream.cancelTask();
- queue.deinit();
+ l.core.stream.cancelTask();
+ o.queue.deinit();
+ o.g.hit_core = null;
}
- var pipe_tasks: PipeTasks = .{};
- defer pipe_tasks.cancelAll(io);
- var inotify_fd: c_int = file_watch.init(true);
- var watch_reader: ?std.Thread = null;
- var watch_stop: [2]c_int = .{ -1, -1 };
- defer if (inotify_fd >= 0) {
- if (watch_reader) |thread| {
- _ = host_io.writeFd(watch_stop[1], "x");
- thread.join();
- }
- for (watch_stop) |fd| if (fd >= 0) {
- _ = libc.close(fd);
+
+ pub fn resumePtys(o: *Own, rec: *const upgrade.Record) void {
+ o.shell.resumePtys(rec);
+ }
+
+ pub fn start(o: *Own, l: *Session) !void {
+ for (&o.ptys, 0..) |*slot, id| if (slot.*) |*pt| {
+ pt.reader = try o.queue.startReader(.{ .handle = pt.fd, .flags = .{ .nonblocking = false } }, @intCast(id), o.gens[id]);
};
- file_watch.stop(inotify_fd);
- _ = libc.close(inotify_fd);
- inotify_fd = -1;
- };
- var watches: file_watch.Table = @splat(null);
- var fs = ninep_io.start(io, gpa, core);
- defer if (fs) |f| f.deinit(gpa);
+ if (o.inotify_fd >= 0) {
+ o.watch_stop = try stopPipe();
+ o.watch_reader = try std.Thread.spawn(.{}, watchThread, .{ o.inotify_fd, o.watch_stop[0], &o.queue });
+ }
+ if (l.fs) |f| try f.watch(&o.queue, wakeFs);
- var shell: Shell = .{
- .core = core,
- .gui = g,
- .io = io,
- .gpa = gpa,
- .lsp_allocator = allocs.lsp,
- .prompt_rcs = &prompt_rcs,
- .ptys = &ptys,
- .gens = &gens,
- .queue = &queue,
- .pipe_tasks = &pipe_tasks,
- .inotify_fd = inotify_fd,
- .watches = &watches,
- .fs = fs,
- .test_mode = test_mode,
- .test_clock = if (test_mode) host_io.testClock() else null,
- .test_hz = if (std.c.getenv("PARDES_TEST_HZ")) |hz| @max(1, std.fmt.parseInt(u64, std.mem.span(hz), 10) catch 60) else 60,
- };
- defer shell.job_tasks.cancelAll(io);
- defer shell.shutdownPtys();
- // A virtual clock never blinks: goldens and feel series hold still.
- g.no_blink = shell.test_clock != null;
- frame_probe = &frames;
- frame_capture_dir = if (std.c.getenv("PARDES_FRAMES_CAPTURE")) |dir| std.mem.span(dir) else null;
- const host = shell.host();
- core.host = host;
- pardes.Kind.drawer = .{ .ctx = &shell, .go = kindDraw };
- defer pardes.Kind.drawer = null;
- shell.peers = .{ .gpa = gpa, .io = io, .name = daemon.name(), .wake = sdlWake, .detached = detached };
- defer shell.peers.deinit();
- // Before the core goes: each client sees its link end.
- defer shell.peers.dropAll(shell.core);
+ _ = c.SDL_StartTextInput(o.g.window);
+ // An upgraded session's window, its view's again.
+ if (upgrade.resumed) |*rec| if (!rec.fresh and !o.detached) o.shell.resumeWindow();
- while (core.nextEffect()) |e| core.perform(e);
- // An upgraded session's shells, once a fresh session's own has a
- // directory to adopt them in.
- if (upgrade.resumed) |*rec| {
- shell.resumePtys(rec);
- while (core.nextEffect()) |e| core.perform(e);
+ if (o.test_mode) setStdinRaw() catch {};
+ o.shell.threads_ok = true;
+ pardes.lsp.setStatusSink(&o.queue, Queue.lspStatus);
+ host_io.setExitWake(&o.queue, Queue.wakeForExit);
+ if (o.detached) {
+ o.shell.closeTheWindow();
+ // Nothing else may wake the first wait.
+ l.watch();
+ }
}
- for (&ptys, 0..) |*slot, id| if (slot.*) |*pt| {
- pt.reader = try queue.startReader(.{ .handle = pt.fd, .flags = .{ .nonblocking = false } }, @intCast(id), gens[id]);
- };
- if (inotify_fd >= 0) {
- watch_stop = try stopPipe();
- watch_reader = try std.Thread.spawn(.{}, watchThread, .{ inotify_fd, watch_stop[0], &queue });
+
+ pub fn sent(_: *Own) void {}
+
+ pub fn upgradeAsked(o: *Own) void {
+ if (o.shell.upgrade_asked) |pane| o.shell.upgradeNow(pane);
}
- if (fs) |f| try f.watch(&queue, wakeFs);
- _ = c.SDL_StartTextInput(g.window);
- // An upgraded session's window, its view's again.
- if (upgrade.resumed) |*rec| if (!rec.fresh and !detached) shell.resumeWindow();
+ pub fn note(o: *Own, n: daemon.Note) void {
+ deliverNote(&o.shell, n);
+ }
- if (test_mode) setStdinRaw() catch {};
- shell.threads_ok = true;
- pardes.lsp.setStatusSink(&queue, Queue.lspStatus);
- defer pardes.lsp.setStatusSink(null, null);
- host_io.setExitWake(&queue, Queue.wakeForExit);
- defer host_io.setExitWake(null, null);
- if (detached) {
- shell.closeTheWindow();
- // Nothing else may wake the first wait.
- daemon.watch(&shell, deliverNote);
+ pub fn watched(o: *Own) session_loop.Watched {
+ return .{ .fd = o.inotify_fd, .table = &o.watches };
}
- while (!core.quit) {
- pardes.turn.bump(&pardes.turn.restores);
- try core.pump(host);
- // Its unit to each replica client, and theirs taken in.
- shell.peers.boundary(core);
- upgrade.locate();
- // Asked in the step (the word), done between steps: the session whole.
- if (shell.upgrade_asked) |pane| shell.upgradeNow(pane);
- // A window asked for again, or an adopted shell, from the daemon's
- // thread through the queue; started after the first frame.
- daemon.watch(&shell, deliverNote);
- if (core.quit) break; // a session that ended does not restore into one
- if (core.takeRestore()) |rp| blk: {
- const nc = host_io.restoreCore(gpa, core, rp, &shell.peers, inotify_fd, &watches) orelse break :blk;
- pipe_tasks.cancelAll(io);
- queue.discardCompletions();
- // One view, the window's (host_io.restoreCore).
- shell.gview = 0;
- shell.stopPtys();
- g.cells.clearImages();
- g.prepared_images.clearRetainingCapacity();
- nc.view().frontend.native_images = true;
- nc.view().frontend.native_image_patches = true;
- nc.host = host;
- if (fs) |f| f.reset(nc);
- nc.view().frontend.windowed = true;
- core.deinit();
- core = nc;
- g.hit_core = nc;
- shell.core = nc;
- shell.surface = null;
- observeGuiFont(g, core);
- syncTaglineFont(g, core);
- }
+ pub fn restoring(o: *Own, nc: *pardes.Pardes) void {
+ o.pipe_tasks.cancelAll(o.shell.io);
+ o.queue.discardCompletions();
+ // One view, the window's (host_io.restoreCore).
+ o.shell.gview = 0;
+ o.shell.stopPtys();
+ o.g.cells.clearImages();
+ o.g.prepared_images.clearRetainingCapacity();
+ nc.view().frontend.native_images = true;
+ nc.view().frontend.native_image_patches = true;
+ nc.view().frontend.windowed = true;
}
- if (fs) |f| {
- f.deinit(gpa);
- fs = null;
- shell.fs = null;
+ pub fn restored(o: *Own, l: *Session) void {
+ o.g.hit_core = l.core;
+ o.shell.core = l.core;
+ o.shell.surface = null;
+ observeGuiFont(o.g, l.core);
+ syncTaglineFont(o.g, l.core);
}
-}
+};
/// A window's loop as a client of a session elsewhere (runtime/client.zig),
/// over the window (`Client`).
@@ -1525,6 +1505,7 @@ fn clientSession(init: std.process.Init, g: *Gui, opts_in: pardes.Options, test_
var pipe_tasks: PipeTasks = .{};
var watches: file_watch.Table = @splat(null);
const prompt_rcs: host_io.Shell.PromptFiles = .{};
+ var no_peers: replica.Peers = .{};
var shell: Shell = .{
.core = start.r.core(),
.gui = g,
@@ -1540,6 +1521,7 @@ fn clientSession(init: std.process.Init, g: *Gui, opts_in: pardes.Options, test_
.watches = &watches,
.test_mode = test_mode,
.client = &window,
+ .peers = &no_peers,
};
window.shell = &shell;
pardes.Kind.drawer = .{ .ctx = &shell, .go = kindDraw };
@@ -2490,8 +2472,9 @@ const Shell = struct {
/// This window a replica client of a session elsewhere (`runClient`).
client: ?*Client = null,
/// The session's replica clients (core/replica.zig), on sockets: views
- /// of their own with no window here.
- peers: replica.Peers = .{},
+ /// of their own with no window here; 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 (tui.zig's `mail_only`).
@@ -8173,7 +8156,8 @@ test "a detached GUI authority follows terminal directory changes with no window
const rcs: host_io.Shell.PromptFiles = .{};
var pipes: PipeTasks = .{};
var watches: file_watch.Table = @splat(null);
- var shell: Shell = .{ .core = core, .io = io, .gpa = gpa, .lsp_allocator = gpa, .prompt_rcs = &rcs, .ptys = &ptys, .gens = &gens, .queue = &queue, .pipe_tasks = &pipes, .inotify_fd = -1, .watches = &watches };
+ var no_peers: replica.Peers = .{};
+ var shell: Shell = .{ .core = core, .io = io, .gpa = gpa, .lsp_allocator = gpa, .prompt_rcs = &rcs, .ptys = &ptys, .gens = &gens, .queue = &queue, .pipe_tasks = &pipes, .inotify_fd = -1, .watches = &watches, .peers = &no_peers };
defer shell.shutdownPtys();
const child = try host_io.forkShell(null, 0, &rcs, "/bin/sh", start, 24, 80);
ptys[0] = .{ .fd = child.file.handle, .pid = child.pid, .serial = core.panes[0].?.serial };
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 = .{
diff --git a/runtime/root.zig b/runtime/root.zig
index 61db8cbe..ec89e734 100644
--- a/runtime/root.zig
+++ b/runtime/root.zig
@@ -5,6 +5,7 @@
//! and `core`.
pub const daemon = @import("daemon.zig");
pub const client = @import("client.zig");
+pub const session = @import("session.zig");
pub const upgrade = @import("upgrade.zig");
pub const crash = @import("crash.zig");
pub const addon_test = @import("addon_test.zig");
@@ -14,6 +15,7 @@ pub const ninep = @import("9p.zig");
test {
_ = daemon;
_ = client;
+ _ = session;
_ = upgrade;
_ = ninep_io;
_ = ninep_io.Client;
diff --git a/runtime/session.zig b/runtime/session.zig
new file mode 100644
index 00000000..f401c85a
--- /dev/null
+++ b/runtime/session.zig
@@ -0,0 +1,155 @@
+//! A session's loop: the terminal's (dev/tui `run`) and the window's
+//! (dev/gui `runNative`) alike, each with a frontend of its own. It holds
+//! 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
+//! and its replica clients (`Peers`): 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 devices (the ptys and their readers, the files
+//! watched, the LSP's and the pipes' and addons' jobs, the queue they post
+//! to, the session's own input) are still its frontend's (each `Shell`,
+//! the services step's to make one). 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).
+//! - `open(l) Host`: its devices made over the session's core and parts,
+//! the host the core steps with (the core's, set there); what it does
+//! before the 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 core goes.
+//! - `resumePtys(rec)`: an upgraded session's shells, its own again.
+//! - `start(l)`: after the first effects: 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).
+const std = @import("std");
+const pardes = @import("core");
+const replica = pardes.replica;
+const host_io = @import("dev").host_io;
+const file_watch = @import("dev").file_watch;
+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();
+
+ io: std.Io,
+ gpa: std.mem.Allocator,
+ allocs: pardes.memory.Allocators,
+ /// Its frame timings, the frontend's to write as it draws
+ /// (Pardes.frames).
+ frames: pardes.FrameStats = .{},
+ core: *pardes.Pardes = undefined,
+ prompt_rcs: host_io.Shell.PromptFiles = undefined,
+ fs: ?*ninep_io.Listener = null,
+ peers: replica.Peers = .{},
+ host: pardes.Host = undefined,
+ fe: *Frontend,
+
+ /// The session, from its start to its end.
+ pub fn run(init: std.process.Init, opts: pardes.Options, fe: *Frontend) !void {
+ var l: L = .{ .io = init.io, .gpa = init.gpa, .allocs = pardes.memory.init(init.gpa), .fe = fe };
+ defer pardes.memory.deinit();
+ host_io.startWorkers(l.allocs);
+ defer host_io.stopWorkers();
+ var o = opts;
+ o.addons = pardes.Addon.scan();
+ o.frames = &l.frames;
+ o.image_allocator = l.allocs.image;
+ o.tree_sitter_allocator = l.allocs.tree_sitter;
+ o.frame_allocator = l.allocs.frame;
+ fe.options(&o);
+ l.core = if (upgrade.resumed) |*rec|
+ try upgrade.load(l.gpa, l.allocs.pardes, o, rec)
+ else if (o.load_path) |lp| blk: {
+ const bytes = try pardes.filesystem.readFile(&pardes.tape.none, l.gpa, lp);
+ defer l.gpa.free(bytes);
+ break :blk try pardes.dump.initFromDump(l.allocs.pardes, o, bytes);
+ } else try pardes.Pardes.init(l.allocs.pardes, o);
+ defer l.core.deinit();
+ l.prompt_rcs = host_io.Shell.prepare();
+ defer l.prompt_rcs.deinit();
+ l.fs = ninep_io.start(l.io, l.gpa, l.core);
+ defer if (l.fs) |f| f.deinit(l.gpa);
+ l.peers = .{ .gpa = l.gpa, .io = l.io, .name = daemon.name() };
+
+ l.host = try fe.open(&l);
+ defer fe.close(&l);
+ defer l.peers.deinit();
+ // Before the core goes: each client sees its link end.
+ defer l.peers.dropAll(l.core);
+ l.effects();
+ // ...and its shells, once a fresh session's own has a directory
+ // to adopt them in.
+ if (upgrade.resumed) |*rec| {
+ fe.resumePtys(rec);
+ l.effects();
+ }
+ try fe.start(&l);
+
+ while (!l.core.quit) {
+ pardes.turn.bump(&pardes.turn.restores);
+ try l.core.pump(l.host);
+ // Its unit to each replica client, and theirs taken in.
+ l.peers.boundary(l.core);
+ fe.sent();
+ upgrade.locate();
+ // Asked in the step (the word), done between steps: the
+ // session whole.
+ fe.upgradeAsked();
+ // A terminal (`pardes --attach`), a window asked for again,
+ // an adopted shell or a client's connection, handed on by
+ // the daemon's thread; started after the first frame, off a
+ // start's way.
+ l.watch();
+ // A session that ended does not restore into one.
+ if (l.core.quit) break;
+ if (l.core.takeRestore()) |rp| l.restore(rp);
+ }
+ // Its serve threads read the core: they end first.
+ if (l.fs) |f| {
+ f.deinit(l.gpa);
+ l.fs = null;
+ }
+ }
+
+ fn effects(l: *L) void {
+ while (l.core.nextEffect()) |e| l.core.perform(e);
+ }
+
+ /// What the daemon says, onto the frontend's queue (once: its
+ /// reader is started by the first call).
+ pub fn watch(l: *L) void {
+ daemon.watch(l.fe, note);
+ }
+
+ fn note(ctx: *anyopaque, n: daemon.Note) void {
+ const fe: *Frontend = @ptrCast(@alignCast(ctx));
+ fe.note(n);
+ }
+
+ /// A Restore: the core its dump makes replaces this one, its clients
+ /// let go (host_io.restoreCore); 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;
+ l.fe.restoring(nc);
+ nc.host = l.host;
+ if (l.fs) |f| f.reset(nc);
+ l.core.deinit();
+ l.core = nc;
+ l.fe.restored(l);
+ }
+ };
+}