From ae3b9e9d19492c827f257da8a224a19d77de51c1 Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Fri, 20 Nov 2026 05:58:08 -0300 Subject: last word: a session's last frame on a link says which of two things happened (wire `ended CODE` 0x0b and `left NAME` 0x0c, working names): a Detach typed in a client queues `left` and lets its writer end after it (Peers.detach, Peer.last), a session's end tells every client `ended` with its code before the links are let go (Peers.ended, from runtime/session.zig after the loop; refuseAll and it share the 200 ms flush); a client that typed Detach prints "pardes: detached from 'NAME', which is still running; come back with `pardes attach NAME`" and exits 0 again (the attach's regression to exit 1 and "let this client go" undone, for every client, no variable), a client whose session ended exits with the session's code quietly, a pair's client after reaping its session (EDITOR=pardes waits for the session's end); client.Ended a union (left, ended CODE); a Detach with no daemon is refused in a client too (tui.zig `detach`: the daemon checked first), since a pair with none ends with its last link; test/daemon.py: the client Detach expectations (0 and the line), a session's Exit (0, no let-go line), `pair` Exit 0 and the session reaped, `pair_rows` the rows of the design's tables 1.1 and 2.2 B (Detach said, exit 0, the session listed; a client ended by TERM with its terminal put back, by KILL and by its terminal's HUP: the session runs on; no daemon: Detach refused, the terminal closed ends the session; nothing left); docs/next-run.md and core/CHANGELOG.md say so (the CHANGELOG's `new --detach` line is the first change's); `kept` NOT built: no terminal behaviour turns on it while a hangup and a killed client are losses and Exit ends the session (see the report) Co-Authored-By: Claude Opus Claude-Session: https://claude.ai/code/session_016Y9MNBECMWcJD5pdS4SR6T --- dev/gui/gui.zig | 8 +++++++- dev/tui/tui.zig | 26 +++++++++++++++++++++----- 2 files changed, 28 insertions(+), 6 deletions(-) (limited to 'dev') diff --git a/dev/gui/gui.zig b/dev/gui/gui.zig index ab33abe9..59e70935 100644 --- a/dev/gui/gui.zig +++ b/dev/gui/gui.zig @@ -1356,7 +1356,7 @@ const Client = struct { defer cl.lock.unlock(cl.io); for (cl.inbox.items) |msg| switch (msg) { .unit, .screen, .refused, .blob => |b| std.heap.c_allocator.free(b), - .checked => {}, + .checked, .ended, .left => {}, }; cl.inbox.clearRetainingCapacity(); } @@ -1444,6 +1444,12 @@ fn clientSession(init: std.process.Init, g: *Gui, opts_in: pardes.Options, test_ .done => {}, .gone => Said.no(io, "'{s}' let this client go (a Detach in it, or the session's end)", .{to.session}), .failed => Said.no(io, "{s}", .{start.said()}), + .left => { + var said: [600]u8 = undefined; + std.Io.File.stderr().writeStreamingAll(io, std.fmt.bufPrint(&said, "pardes-gui: detached from '{s}', which is still running; come back with `pardes-gui attach {s}`\n", .{ to.session, to.session }) catch "pardes-gui: detached\n") catch {}; + std.process.exit(0); + }, + .ended => |code| std.process.exit(code), } } diff --git a/dev/tui/tui.zig b/dev/tui/tui.zig index 543be3aa..d752c627 100644 --- a/dev/tui/tui.zig +++ b/dev/tui/tui.zig @@ -710,7 +710,7 @@ const Tty = struct { while (t.loop.tryEvent() catch null) |ev| switch (ev) { .said => |msg| switch (msg) { .unit, .screen, .refused, .blob => |b| std.heap.c_allocator.free(b), - .checked => {}, + .checked, .ended, .left => {}, }, .paste => |b| t.sh.gpa.free(@constCast(b)), else => {}, @@ -797,6 +797,22 @@ pub fn runClient(init: std.process.Init, opts: pardes.Options, path: []const u8, screen.leave(); Said.no(io, "'{s}' let this client go (a Detach in it, or the session's end)", .{session}); }, + .left => { + screen.leave(); + var said: [600]u8 = undefined; + std.Io.File.stderr().writeStreamingAll(io, std.fmt.bufPrint(&said, "pardes: detached from '{s}', which is still running; come back with `pardes attach {s}`\n", .{ session, session }) catch "pardes: detached\n") catch {}; + std.process.exit(0); + }, + .ended => |code| { + screen.leave(); + // A pair's client is its session's parent: its status once it + // is gone, as a terminal's job ends with its program. + if (opts.pair != null) { + var status: c_int = 0; + while (libc.waitpid(@intCast(start.joined.pid), &status, 0) < 0 and libc.errno(-1) == .INTR) {} + } + std.process.exit(code); + }, .failed => { screen.leave(); Said.no(io, "{s}", .{start.said()}); @@ -1264,10 +1280,10 @@ const Shell = struct { /// and the shell has its terminal again), the session left running. fn detach(ctx: ?*anyopaque) void { const s = of(ctx); - // A replica client's: its link ends. + if (!daemon.kept()) return s.core.reportFailure(s.core.view().active, "Detach: no pardes daemon lists this session (it is its terminal's job)"); + // A replica client's: its last word, and its link ends. if (s.peers.detach(s.core.live)) return; if (s.detached()) return s.core.reportFailure(s.core.view().active, "Detach: detached already (no terminal of its own)"); - if (!daemon.kept()) return s.core.reportFailure(s.core.view().active, "Detach: no pardes daemon lists this session (it is its terminal's job)"); s.leaveTerminal(); } @@ -1608,10 +1624,10 @@ const Shell = struct { }; if (echo) s.focused_ns = @intCast(std.Io.Clock.awake.now(s.io).nanoseconds); c.take(said); - if (said == .refused) return true; + if (said == .refused or said == .ended or said == .left) return true; } else switch (said) { .unit, .screen, .refused, .blob => |b| std.heap.c_allocator.free(b), - .checked => {}, + .checked, .ended, .left => {}, }, .link_gone => if (s.client) |c| { c.link.quit = true; -- cgit v1.3