From 969369d4b84fa1847dc3dee0e737dcd58e56c5e7 Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Fri, 20 Nov 2026 01:46:17 -0300 Subject: 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 Claude-Session: https://claude.ai/code/session_016Y9MNBECMWcJD5pdS4SR6T --- test/daemon.py | 224 +++++++++++++++++++++++++++++++++------------------------ 1 file changed, 130 insertions(+), 94 deletions(-) (limited to 'test') diff --git a/test/daemon.py b/test/daemon.py index e600beb7..31806863 100644 --- a/test/daemon.py +++ b/test/daemon.py @@ -2,8 +2,8 @@ """The per-user daemon end to end (docs/next-daemon.md, phase 3), under a private HOME and runtime directory: -- a terminal's session detached (the stub ends, exit 0) and attached again - from another terminal, N times; +- a terminal's session detached (the stub ends, exit 0), then attached + from another terminal N times, each a client (an attach is always one); - a session killed outright: its shells live on, held by the daemon, and another session adopts them as terminal panes; - a replica's hello on the daemon's stream socket (phase 5): one of this @@ -16,8 +16,8 @@ private HOME and runtime directory: - nothing left behind: no session, shell, daemon or socket. python3 -B test/daemon.py [cycles] - python3 -B test/daemon.py --gui [cycles] (a hidden window; - needs a Wayland display, and a pardes beside it for the refusal) + python3 -B test/daemon.py --gui [cycles] (hidden windows, + offscreen, and a pardes beside it for the terminal's client) python3 -B test/daemon.py --ping [n] a hello of 's build and n pings on a replica socket (one `ssh -L` forwards, say), timed @@ -428,8 +428,8 @@ def clients(binary): wait_for('both clients out of insert mode', lambda: normal_again(ta) and normal_again(tb)) wait_for('client A agrees once quiet', lambda: agreed(root / 'a.check', 0)) wait_for('client B agrees once quiet', lambda: agreed(root / 'b.check', 0)) - # A terminal attached while a client's view is live (its input the - # last): it takes the session's own and is drawn. + # A terminal attached by the session's name alone while a client's + # view is live (its input the last): a client too, drawn. tt = Term(24, 80) terms.append(tt) stub = tt.spawn([binary, '--attach=files'], env) @@ -1395,70 +1395,90 @@ def main(binary, cycles): shell = terminals(client)[0] shell_pid = int(run(client, shell, b'echo $$\n').split(b'\n')[1]) started.append(shell_pid) + # Detach in the terminal it was born with: the stub ends (exit 0, + # its words), the terminal put back; the session runs on detached, + # for good: every attach after it is a client. + client.write(f'/pane/{shell}/exec', b'Detach\n') + assert stub.wait(timeout=10) == 0, stub.returncode + # The stub's last words are in its terminal before it ends, but + # this side's reader may not have them yet. + wait_for('the stub says it detached', lambda: b'detached from \'work\', which is still running; come back with `pardes attach work`' in term.text()) + assert b'\x1b[?1049l' in term.text(), 'the terminal is put back' + term.close() + assert alive(session) and alive(shell_pid) + assert run(client, shell, b'echo still\n').startswith(b'exit 0\nstill'), 'the shell runs on, detached' + + def attach(term, argv, typed): + """A client: its own process, drawn, listed by --ps under the + session, detached still, and its keys in the shell.""" + c = term.spawn(argv, env) + started.append(c.pid) + wait_for('the client\'s first frame', lambda: b'\x1b[?1049h' in term.text()) + listed = wait_for('the client in --ps', lambda: (lambda out: f'client {c.pid} ' in out and out)( + subprocess.run([binary, '--ps'], env=env, capture_output=True, timeout=30).stdout.decode())) + assert f'session work {session} (tty, detached)' in listed, listed + os.write(term.master, f'echo {typed}\r'.encode()) + wait_for('its keys in the shell', lambda: typed.encode() in client.read(f'/pane/{shell}/body')) + return c + + def let_go(how, c, term): + """Detach in it (its view the live one: it typed last), or its + terminal closed: it ends; the session and its shell run on.""" + if how == 'detach': + client.write(f'/pane/{shell}/exec', b'Detach\n') + assert c.wait(timeout=10) == 1, c.returncode + wait_for('the client says it was let go', lambda: b'let this client go' in term.text()) + else: + term.close() + c.wait(timeout=10) + if how == 'detach': + term.close() + assert alive(session) and alive(shell_pid) + baseline = None for cycle in range(cycles): - client.write(f'/pane/{shell}/exec', b'Detach\n') - assert stub.wait(timeout=10) == 0, stub.returncode - # The stub's last words are in its terminal before it ends, but - # this side's reader may not have them yet. - wait_for('the stub says it detached', lambda: b'detached from \'work\'' in term.text()) - said = term.text() - assert b'\x1b[?1049l' in said, 'the terminal is put back' - term.close() - assert alive(session) and alive(shell_pid) - assert run(client, shell, b'echo still\n').startswith(b'exit 0\nstill'), 'the shell runs on, detached' term = Term(30, 100) - stub = term.spawn([binary, '--attach=work'], env) - wait_for('the attached terminal\'s first frame', lambda: b'\x1b[?1049h' in term.text()) - wait_for('the session sized to its new terminal', - lambda: client.screen()['cols'] == 100 and client.screen()['rows'] == 30) + c = attach(term, [binary, '--attach=work'] if cycle % 2 else [binary, 'attach'], f'cycle-{cycle}') now = (fds(session), fds(daemon), len(list(root.iterdir())), len(processes(root))) if baseline is None: baseline = now assert now == baseline, (cycle, now, baseline) - print(f'daemon: {cycles} detach/attach cycles, fds/sockets/processes level: {baseline}') - - # Another terminal while one is attached: a replica client of the - # session (a session shows one frontend of its own), listed under - # it by --ps, drawn beside the first, typed into as the first is. - # Detached from inside, it ends and the first draws on; closed - # outright, the same. + let_go('detach' if cycle % 2 else 'close', c, term) + print(f'daemon: a Detach in its own terminal, then {cycles} attaches as clients (Detach in each, or its terminal closed), ' + f'fds/sockets/processes level: {baseline}') + + # Two clients at once, both listed by --ps, both typed into and + # drawn; the second let go by a Detach in it, or its terminal + # closed, and the first draws on. + term = Term(30, 100) + first = attach(term, [binary, 'attach', 'work'], 'first-in') for how in ('detach', 'close'): - # Bigger than the first: the shell, sized for the first (its - # last typist), shows whole in it. term2 = Term(40, 120) - stub2 = term2.spawn([binary, '--attach=work'], env) - started.append(stub2.pid) - wait_for('the second terminal\'s first frame', lambda: b'\x1b[?1049h' in term2.text()) - wait_for('the second terminal\'s view live', lambda: client.screen()['cols'] == 120) - listed = subprocess.run([binary, '--ps'], env=env, capture_output=True, timeout=30).stdout.decode() - assert f'client {stub2.pid} ' in listed, listed + second = attach(term2, [binary, 'attach', 'work'], f'two-typed-{how}') os.write(term.master, f'echo one-typed-{how}\r'.encode()) wait_for('the first terminal\'s keys in the shell', lambda: f'one-typed-{how}'.encode() in client.read(f'/pane/{shell}/body')) - os.write(term2.master, f'echo two-typed-{how}\r'.encode()) - wait_for('the second terminal\'s keys in the shell', lambda: f'two-typed-{how}'.encode() in client.read(f'/pane/{shell}/body')) + # (Words no line before has a letter of in the same place: a + # terminal is sent the cells that changed.) + drawn = f'{how.upper()}BOTHDRAWN' seen = len(term.text()) - run(client, shell, f'echo both-drawn-{how}\n'.encode()) - wait_for('the first terminal drawn', lambda: f'both-drawn-{how}'.encode() in term.text()[seen:]) - wait_for('the second terminal drawn', lambda: f'both-drawn-{how}'.encode() in term2.text()) - assert stub.poll() is None and stub2.poll() is None - if how == 'detach': - # Its view is the live one (its terminal spoke last). - client.write(f'/pane/{shell}/exec', b'Detach\n') - stub2.wait(timeout=10) - term2.close() - else: - term2.close() - stub2.wait(timeout=10) - wait_for('the first terminal\'s view live again', lambda: client.screen()['cols'] == 100) - wait_for('the client gone from --ps', lambda: 'client ' not in subprocess.run([binary, '--ps'], env=env, capture_output=True, timeout=30).stdout.decode()) + run(client, shell, f'echo {drawn}\n'.encode()) + wait_for('the first terminal drawn', lambda: drawn.encode() in term.text()[seen:]) + wait_for('the second terminal drawn', lambda: drawn.encode() in term2.text()) + assert first.poll() is None and second.poll() is None + # Its view the live one: it typed last. + os.write(term2.master, f'echo two-last-{how}\r'.encode()) + wait_for('the second terminal\'s keys in the shell', lambda: f'two-last-{how}'.encode() in client.read(f'/pane/{shell}/body')) + let_go(how, second, term2) + wait_for('the client gone from --ps', lambda: f'client {second.pid} ' not in subprocess.run([binary, '--ps'], env=env, capture_output=True, timeout=30).stdout.decode()) + drawn = f'{how.upper()}FIRSTON' seen = len(term.text()) - run(client, shell, f'echo first-on-{how}\n'.encode()) - wait_for('the first terminal drawn on', lambda: f'first-on-{how}'.encode() in term.text()[seen:]) - assert stub.poll() is None, 'the first terminal stays attached' + run(client, shell, f'echo {drawn}\n'.encode()) + wait_for('the first terminal drawn on', lambda: drawn.encode() in term.text()[seen:]) + assert first.poll() is None, 'the first client stays' now = (fds(session), fds(daemon), len(list(root.iterdir())), len([p for p in processes(root) if alive(p)])) assert now[:3] == baseline[:3], (how, now, baseline) - print('daemon: a second terminal on an attached session a client of it (in --ps), both typed into and drawn, then detached and closed; the first drew on') + print('daemon: two clients at once, each in --ps, both typed into and drawn; the second detached, then closed; the first drew on') + stub = first # Killed outright: its shells live on, their masters the daemon's, # and a replica client of it hears so at once, in words (one froze @@ -1507,22 +1527,33 @@ def main(binary, cycles): shell.kill() shell.wait(timeout=10) term.close() - term = Term() - stub = term.spawn([binary, '--attach=fresh'], env) - wait_for('the attached terminal\'s first frame', lambda: b'\x1b[?1049h' in term.text()) - wait_for('the session sized to its new terminal', lambda: not sized(client)) - print('daemon: a session whose stub was killed outright detached, and was attached again') before = set(terminals(client)) client.write(f'/pane/{min(before)}/exec', b'Adopt\n') adopted = wait_for('the adopted pane', lambda: set(terminals(client)) - before) pane = adopted.pop() + # Sized twice (Ptys.take): a run into a prompt redrawn under it + # reads as not run (core/ninep/pty.zig). + wait_for('the adopted shell answers', lambda: run(client, pane, b'true\n') == b'exit 0\n') said = run(client, pane, b'echo $$\n') assert said.split(b'\n')[1] == str(shell_pid).encode(), said print(f'daemon: shell {shell_pid} outlived kill -9 of its session and was adopted') - - # The end: the session quits, its shells hang up, the daemon goes. - client.write(f'/pane/{pane}/exec', b'Exit\n') - stub.wait(timeout=10) + # Attached again: a client. (After the runs: a pty/run under a + # client puts it off the session's state, and it joins again; one + # that tries as the session ends says it could not.) + term = Term() + stub = term.spawn([binary, 'attach', 'fresh'], env) + started.append(stub.pid) + wait_for('the client\'s first frame', lambda: b'\x1b[?1049h' in term.text()) + listed = wait_for('the client in --ps', lambda: (lambda out: f'client {stub.pid} ' in out and out)( + subprocess.run([binary, '--ps'], env=env, capture_output=True, timeout=30).stdout.decode())) + assert f'session fresh {session} (tty, detached)' in listed, listed + print('daemon: a session whose stub was killed outright detached, and was attached again, as a client') + + # The end: the session quits, its shells hang up, its client and + # the daemon go. + exit_session(client, pane) + assert stub.wait(timeout=10) == 1, stub.returncode + wait_for('the client says it was let go', lambda: b'let this client go' in term.text()) client.socket.close() term.close() wait_for('the adopted shell hung up', lambda: not alive(shell_pid)) @@ -1551,62 +1582,67 @@ def sized(client): def gui(binary, cycles): - """A window's Detach closes it and keeps the process; `pardes-gui - --attach` opens it again. Test mode: a hidden window, its stdin the - test feed.""" + """A window's Detach closes it and keeps the process, detached for good: + `pardes-gui attach` is a window client of it, a process of its own, + and so is a terminal's `pardes attach`. Test mode: hidden windows, + offscreen, the session's stdin the test feed.""" scratch = Path(__file__).resolve().parent.parent.parent / '.scratch' / 'daemon' / 'dmn' + scratch.mkdir(parents=True, exist_ok=True) root = Path(tempfile.mkdtemp(prefix='rtg-', dir=scratch)) env = {k: v for k, v in os.environ.items() if not k.startswith('PARDES_') and 'PAGER' not in k} - display = Path(os.environ.get('WAYLAND_DISPLAY', 'wayland-0')) - if not display.is_absolute(): - display = Path(os.environ.get('XDG_RUNTIME_DIR', f'/run/user/{os.getuid()}')) / display (root / 'caps').mkdir() env.update(HOME=str(root), XDG_RUNTIME_DIR=str(root), XDG_CONFIG_HOME=str(root / 'config'), SHELL='/bin/bash', PARDES_NOTIME='1', PARDES_TEST='1', PARDES_TEST_CAPTURE_DIR=str(root / 'caps'), - SDL_VIDEODRIVER='wayland', WAYLAND_DISPLAY=str(display), PARDES_DAEMON='on') + SDL_VIDEO_DRIVER='offscreen', PARDES_DAEMON='on', TERM='xterm-256color') (root / 'config' / 'pardes').mkdir(parents=True) (root / 'config' / 'pardes' / 'init').write_text('Shell bash\nMessageLinger 0\n') feed_r, feed_w = os.pipe() proc = subprocess.Popen([binary, '--9p=win'], env=env, cwd=root, stdin=feed_r, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL, start_new_session=True) os.close(feed_r) + + def listed(): + return subprocess.run([binary, '--ps'], env=env, cwd=root, capture_output=True, timeout=30).stdout.decode() + try: sock = root / 'pardes-9p-win.sock' wait_for('the window session', sock.exists, timeout=20) client = Client(sock) shell = terminals(client)[0] assert not sized(client), 'a window owns the size' - baseline = None - for cycle in range(cycles): - client.write(f'/pane/{shell}/exec', b'Detach\n') - wait_for('the window closed', lambda: sized(client)) - assert proc.poll() is None, 'the process carries on with no window' - attach = subprocess.run([binary, '--attach=win'], env=env, cwd=root, timeout=10, - stdout=subprocess.PIPE, stderr=subprocess.PIPE) - assert attach.returncode == 0, attach.stderr - wait_for('the window open again', lambda: not sized(client)) - now = (fds(proc.pid), len(processes(root))) - baseline = baseline or now - assert now == baseline, (cycle, now, baseline) - print(f'daemon: {cycles} window detach/attach cycles, fds/processes level: {baseline}') - # Attached already: another window is a replica client of the - # session (its own process, its captures its own), listed by --ps; - # closed, it ends alone. + # With its window: another window is a client of the session (its + # own process, its captures its own), listed by --ps; closed, it + # ends alone. second = Window(root, 'second', [binary, '--attach=win'], env) wait_for('the second window drawn', lambda: (root / 'second' / 'latest.ppm').exists(), timeout=20) - listed = subprocess.run([binary, '--ps'], env=env, cwd=root, capture_output=True, timeout=30).stdout.decode() - assert f'client {second.pid} ' in listed, listed + assert f'client {second.pid} ' in listed(), listed() second.close() - wait_for('the client gone from --ps', lambda: 'client ' not in subprocess.run([binary, '--ps'], env=env, cwd=root, capture_output=True, timeout=30).stdout.decode()) + wait_for('the client gone from --ps', lambda: 'client ' not in listed()) assert not sized(client), 'the first window stays' assert proc.poll() is None - print('daemon: a second window on an attached window session a client of it (in --ps), closed alone') - # Another kind joins as a replica client: a terminal of the window's - # session, by its name alone. A name nobody has is refused in words. + print('daemon: a second window on a window session a client of it (in --ps), closed alone') + # Detached: the window closed, the process on; every attach after it + # a window client, the session detached still. client.write(f'/pane/{shell}/exec', b'Detach\n') wait_for('the window closed', lambda: sized(client)) + assert proc.poll() is None, 'the process carries on with no window' + assert f'session win {proc.pid} (gui, detached)' in listed(), listed() + baseline = None + for cycle in range(cycles): + w = Window(root, f'w{cycle}', [binary, 'attach'] if cycle % 2 else [binary, '--attach=win'], env) + wait_for('the client window drawn', lambda: (root / f'w{cycle}' / 'latest.ppm').exists(), timeout=20) + wait_for('the client in --ps', lambda: f'client {w.pid} ' in listed()) + assert f'session win {proc.pid} (gui, detached)' in listed(), listed() + now = (fds(proc.pid), len(processes(root))) + baseline = baseline or now + assert now == baseline, (cycle, now, baseline) + w.close() + wait_for('the client gone from --ps', lambda: 'client ' not in listed()) + print(f'daemon: a window Detach, then {cycles} window attaches as clients, fds/processes level: {baseline}') + # A terminal's client of the window's session, by its name alone. A + # name nobody has is refused in words. term = Term() - tty_attach = term.spawn([binary.replace('pardes-gui', 'pardes'), '--attach=win'], env) + tty_attach = term.spawn([binary.replace('pardes-gui', 'pardes'), 'attach', 'win'], env) wait_for('the window session drawn in the terminal', lambda: b'\x1b[?1049h' in term.text()) time.sleep(1) assert tty_attach.poll() is None, term.text()[-400:] -- cgit v1.3