diff options
| author | Gabriel Schneider <[email protected]> | 2026-11-20 01:46:17 -0300 |
|---|---|---|
| committer | Gabriel Schneider <[email protected]> | 2026-11-20 04:00:32 -0300 |
| commit | 969369d4b84fa1847dc3dee0e737dcd58e56c5e7 (patch) | |
| tree | e866b74b33701005eb2009ce573aef8dd4ab420f /runtime/main.zig | |
| parent | 11f55f018a5eb0699d58627a4f2ea39c2b7f5e89 (diff) | |
| download | pardes-969369d4b84fa1847dc3dee0e737dcd58e56c5e7.tar.gz pardes-969369d4b84fa1847dc3dee0e737dcd58e56c5e7.zip | |
attach: `pardes attach [NAME]` is a client always, from a terminal a terminal's and from `pardes-gui` a window's, whatever the session was born with (the ruling of 2026-11-17: a session's own frontend is only the one it was born with), and the in-place attach deleted with what only it used (305 lines deleted, 94 added): the daemon's `attach` request and its `find` that chose (runtime/daemon.zig), `daemon.attach` and its `Answer`, `Note.attach`, the stub's second use (`stubWait`'s `child` and its `r`/`x` words, `tellStubExit`), `attachTo`'s two hand-offs and `crossAttach` (runtime/main.zig: a named attach goes straight to the replica socket, where the daemon refuses a name nobody has; a bare one takes the one detached session, else the one session, from the daemon's `list`, else names them: `bareAttach`, with a test), the terminal handed to a session (`attachTerminal`, `refuse`, the `.attach` message and its three arms: dev/tui/tui.zig), the window opened again in place (`openTheWindow`, `Shell.detached`, the queue's `.window` and its arms: dev/gui/gui.zig, dev/Inbox.zig, runtime/macos.zig), and `setState(false)`, which nothing says now (`setDetached`); test/daemon.py's attach cases expect a client (a Detach in its own terminal once, then each attach a client in `--ps`, the session detached still, its Detach exit 1 "let this client go"; the window cycles as window clients, offscreen); tui shell-test 1046/1049 (3 skipped, as the base), core-test 855/1005 (the base's less the deleted `find` test), daemon.py plain passing (gated with the next change)
Co-Authored-By: Claude Opus <[email protected]>
Claude-Session: https://claude.ai/code/session_016Y9MNBECMWcJD5pdS4SR6T
Diffstat (limited to 'runtime/main.zig')
| -rw-r--r-- | runtime/main.zig | 89 |
1 files changed, 49 insertions, 40 deletions
diff --git a/runtime/main.zig b/runtime/main.zig index 448dfa57..563461ed 100644 --- a/runtime/main.zig +++ b/runtime/main.zig @@ -1595,7 +1595,6 @@ fn nativeMain(std_init: std.process.Init) !void { } } defer daemon.quit(); - errdefer daemon.tellStubExit(1); switch (pardes.platform) { .tui => @import("dev").tui.run(init, opts, term) catch |err| switch (err) { // No controlling terminal (a detached pty, a daemon): said, not @@ -1609,7 +1608,6 @@ fn nativeMain(std_init: std.process.Init) !void { .gui => try @import("dev").gui.run(init, opts, detached), .macos => unreachable, } - daemon.tellStubExit(0); } extern "c" fn execvp(file: [*:0]const u8, argv: [*:null]const ?[*:0]const u8) c_int; @@ -1803,50 +1801,61 @@ fn attachClient(init: std.process.Init, opts: pardes.Options, where: []const u8, std.process.exit(0); } -/// `pardes --attach[=name]` in a terminal: this terminal handed (through -/// the daemon) to the detached session of that name, or the only one, and -/// this process its stub until the session leaves it. `pardes-gui -/// --attach`: the session asked to open its window again, and done. A -/// session attached already, or of the other kind, is joined as a replica -/// client, through this machine's replica socket. +/// `pardes attach [NAME]` (`pardes-gui` too): a client of the session of +/// that name on this machine, through its replica socket (the daemon +/// refuses a name no session has); bare, of the one `bareAttach` finds. fn attachTo(init: std.process.Init, opts: pardes.Options, name: []const u8) noreturn { const io = init.io; - var words: [600]u8 = undefined; if (comptime !daemon.supported) die(io, "--attach: this system has no pardes daemon to attach through", .{}); - if (pardes.platform == .gui) switch (daemon.attach(.gui, name, &.{}, &words)) { - .ok => std.process.exit(0), - .no => |why| die(io, "--attach: {s}", .{why}), - .other => |named| crossAttach(init, opts, named), - }; - const fd = std.c.open("/dev/tty", .{ .ACCMODE = .RDWR, .CLOEXEC = true, .NOCTTY = true }, @as(std.c.mode_t, 0)); - if (fd < 0) die(io, "--attach: no terminal to attach: /dev/tty will not open", .{}); - if (std.posix.tcgetattr(fd)) |tio| std.posix.tcsetattr(fd, .NOW, tio) catch {} else |_| {} - var pipe: [2]std.c.fd_t = undefined; - if (std.c.pipe(&pipe) != 0) die(io, "--attach: no pipe for the session to say when it leaves", .{}); - ninep_io.setCloexec(pipe[0]); - ninep_io.setCloexec(pipe[1]); - switch (daemon.attach(.tty, name, &.{ fd, pipe[1] }, &words)) { - .ok => |pid| { - _ = std.c.close(fd); - _ = std.c.close(pipe[1]); - daemon.stubWait(pid, false, pipe[0]); - }, - .no => |why| die(io, "--attach: {s}", .{why}), - .other => |named| { - _ = std.c.close(fd); - _ = std.c.close(pipe[0]); - _ = std.c.close(pipe[1]); - crossAttach(init, opts, named); - }, + var list: [4096]u8 = undefined; + var names_buf: [600]u8 = undefined; + var names: std.Io.Writer = .fixed(&names_buf); + const said = daemon.ask("list", &list) orelse die(io, "--attach: no pardes daemon is running", .{}); + const named = if (name.len != 0) name else bareAttach(said, &names) orelse if (names.end == 0) + die(io, "--attach: no session is running", .{}) + else + die(io, "--attach: say which session:{s}", .{names.buffered()}); + var sock_buf: [ninep_io.sun_path_len:0]u8 = undefined; + const sock = daemon.replicaSocket(&sock_buf) orelse die(io, "--attach: no runtime directory for the replica socket", .{}); + attachClient(init, opts, sock, named); +} + +/// The session a bare attach takes, from the daemon's `list`: the one +/// detached, else the one there is; null with none or several, every +/// session's name in `names`. +fn bareAttach(list: []const u8, names: *std.Io.Writer) ?[]const u8 { + var one: ?[]const u8 = null; + var detached: ?[]const u8 = null; + var n: usize = 0; + var n_detached: usize = 0; + var lines = std.mem.tokenizeScalar(u8, list, '\n'); + while (lines.next()) |line| { + var words = std.mem.tokenizeScalar(u8, line, ' '); + if (!std.mem.eql(u8, words.next() orelse "", "session")) continue; + _ = words.next(); // its pid + _ = words.next(); // its kind + const state = words.next() orelse continue; + one = std.mem.trim(u8, words.rest(), " "); + n += 1; + names.print(" {s}", .{one.?}) catch {}; // cut short + if (!std.mem.eql(u8, state, "detached")) continue; + detached = one; + n_detached += 1; } + if (n_detached == 1) return detached; + return if (n == 1) one else null; } -/// A session attached already or of the other kind (`--attach`'s): a -/// replica client of it here. -fn crossAttach(init: std.process.Init, opts: pardes.Options, name: []const u8) noreturn { - var sock_buf: [ninep_io.sun_path_len:0]u8 = undefined; - const sock = daemon.replicaSocket(&sock_buf) orelse die(init.io, "--attach: no runtime directory for the replica socket", .{}); - attachClient(init, opts, sock, name); +test "a bare attach takes the one detached session, else the one there is, else names them" { + var buf: [600]u8 = undefined; + var names: std.Io.Writer = .fixed(&buf); + try std.testing.expectEqualStrings("b", bareAttach("daemon 1\nsession 2 tty attached a\nshell 9\nsession 3 gui detached b\n", &names).?); + try std.testing.expectEqualStrings("a", bareAttach("daemon 1\nsession 2 tty attached a\n", &names).?); + names = .fixed(&buf); + try std.testing.expect(bareAttach("daemon 1\norphan 4\n", &names) == null); + try std.testing.expectEqual(@as(usize, 0), names.end); + try std.testing.expect(bareAttach("session 2 tty detached a\nsession 3 tty detached b c\n", &names) == null); + try std.testing.expectEqualStrings(" a b c", names.buffered()); } /// Where Recent's file (0.24) and the store before it were kept: |
