diff options
| author | Gabriel Schneider <[email protected]> | 2026-11-20 08:36:06 -0300 |
|---|---|---|
| committer | Gabriel Schneider <[email protected]> | 2026-11-20 08:36:06 -0300 |
| commit | 446cde19a6b6fb7125fc749608cc12b7452dad6e (patch) | |
| tree | a7c2792ecdb1833a2e5a3ef99b9fb6ce96c01a00 /test | |
| parent | 2c2800c93e5417297dd06d3e52d5d336d975560c (diff) | |
| parent | ae3b9e9d19492c827f257da8a224a19d77de51c1 (diff) | |
| download | pardes-446cde19a6b6fb7125fc749608cc12b7452dad6e.tar.gz pardes-446cde19a6b6fb7125fc749608cc12b7452dad6e.zip | |
integration: a session's first client over an inherited socket before its first effects, the pair under PARDES_PAIR, and a session's last word, on the 9P run's fixes and the build's second pass: pardes-first-client (new --detach forks and returns; Options.pair and the join by fd; pardes FILE as a client and a session under the variable; ended CODE and left NAME: a client's own Detach exits 0 with its line again) and pardes-merge33, merged without a textual conflict; tui core-test 858/1013 (155 skipped), shell-test 1053/1056, the window builds; the coordinator's gate in the ledger (reorg/merge34)
Co-Authored-By: Claude Fable 5.2 <[email protected]>
Claude-Session: https://claude.ai/code/session_016Y9MNBECMWcJD5pdS4SR6T
Diffstat (limited to 'test')
| -rw-r--r-- | test/daemon.py | 256 | ||||
| -rw-r--r-- | test/process.zig | 3 |
2 files changed, 250 insertions, 9 deletions
diff --git a/test/daemon.py b/test/daemon.py index d134732b..7dc1b4bd 100644 --- a/test/daemon.py +++ b/test/daemon.py @@ -25,6 +25,10 @@ private HOME and runtime directory: `ssh -L` forwards, say), timed python3 -B test/daemon.py --clients <pardes> the replica clients alone + python3 -B test/daemon.py --launch <pardes> + `pardes new --detach` returns once its session runs + python3 -B test/daemon.py --pair <pardes> + PARDES_PAIR: a terminal's session and its first client python3 -B test/daemon.py --relink <pardes> the daemon killed: its sessions register again with the next python3 -B test/daemon.py --far <pardes> @@ -479,10 +483,10 @@ def clients(binary): wait_for('client A out of insert mode', lambda: normal_again(ta)) wait_for('client A agrees again', lambda: agreed(root / 'a.check', seen)) assert sess.poll() is None - # Detach typed in it: it alone goes. + # Detach typed in it: it alone goes, and says so (exit 0). os.write(ta.master, b' sD') wait_for('client A let go', lambda: a.poll() is not None, timeout=10) - assert b'let this client go' in ta.text(), ta.text()[-300:] + assert a.returncode == 0 and b"detached from 'files', which is still running" in ta.text(), ta.text()[-300:] # A third client, a shell: its keys wait for the session (a pty # write), its output comes back in units, the same on both sides. nine.write(f'/pane/{pane}/exec', b'Term\n') @@ -526,6 +530,234 @@ def clients(binary): shutil.rmtree(root, ignore_errors=True) +def launch(binary): + """`pardes new --detach=NAME` returns once its session is registered + and listening: exit 0, the name on stdout, the session a process of its + own in a session of its own, listed by `ps` and answering 9P at once; + bare `--detach`, its pid is its name; a name taken is refused in words, + exit 1; `pardes --detach=NAME` (no `new`) is still the session itself; + nothing left behind.""" + scratch = Path(__file__).resolve().parent.parent.parent / '.scratch' / 'daemon' / 'dmn' + scratch.mkdir(parents=True, exist_ok=True) + root = Path(tempfile.mkdtemp(prefix='rl-', dir=scratch)) + env = {k: v for k, v in os.environ.items() if not k.startswith('PARDES_') and 'PAGER' not in k} + env.update(HOME=str(root), XDG_RUNTIME_DIR=str(root), XDG_CONFIG_HOME=str(root / 'config'), + TERM='xterm-256color', SHELL=shutil.which('bash') or '/bin/bash', PARDES_NOTIME='1') + (root / 'config' / 'pardes').mkdir(parents=True) + (root / 'config' / 'pardes' / 'init').write_text('Shell bash\nMessageLinger 0\n') + (root / 'notes.txt').write_text('notes\n') + + def cli(*args, timeout=30): + return subprocess.run([binary, *args], env=env, cwd=root, capture_output=True, timeout=timeout, + stdin=subprocess.DEVNULL) + + def status_pid(name): + with Client(root / f'pardes-9p-{name}.sock') as c: + return int(dict(line.split(maxsplit=1) for line in c.read('/status').decode().splitlines())['pid']) + + try: + t0 = time.monotonic() + try: + out = cli('new', '--detach=work', 'notes.txt', timeout=15) + except subprocess.TimeoutExpired: + raise AssertionError(f'launch: `new --detach=work` had not returned after {time.monotonic() - t0:.1f} s') + took = time.monotonic() - t0 + assert out.returncode == 0 and out.stdout == b'work\n', out + # Registered and listening when it returned: no wait here. + assert (root / 'pardes-9p-work.sock').exists(), list(root.iterdir()) + pid = status_pid('work') + listed = cli('ps').stdout.decode() + assert f'session work {pid} (tty, detached)' in listed, listed + assert alive(pid) and os.getsid(pid) == pid, (pid, os.getsid(pid)) + out = cli('new', '--detach') + assert out.returncode == 0 and out.stdout.strip().isdigit(), out + unnamed = int(out.stdout) + assert status_pid(str(unnamed)) == unnamed and f'session {unnamed} {unnamed} (tty, detached)' in cli('ps').stdout.decode() + taken = cli('new', '--detach=work') + assert taken.returncode == 1 and taken.stderr == b'pardes: a session named work is already running\n', taken + # The flag alone runs the session in this process, as a supervisor + # or a harness wants it. + fg = subprocess.Popen([binary, '--detach=fg'], env=env, cwd=root, stdin=subprocess.DEVNULL, + stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL, start_new_session=True) + wait_for('the foreground session\'s socket', (root / 'pardes-9p-fg.sock').exists) + assert status_pid('fg') == fg.pid and fg.poll() is None + print(f'launch: `new --detach=work` returned 0 in {took:.2f} s with its name, session {pid} listed and listening ' + f'in a session of its own; unnamed it said {unnamed}; a taken name refused; `--detach=fg` is its own session') + for name in ('work', str(unnamed), 'fg'): + assert cli('kill', name).returncode == 0 + fg.wait(timeout=10) + wait_for('nothing left', lambda: not [p for p in processes(root) if alive(p)], timeout=10) + print('launch: nothing left behind') + finally: + for p in processes(root): + try: + os.kill(p, signal.SIGKILL) + except OSError: + pass + shutil.rmtree(root, ignore_errors=True) + + +def pair(binary): + """PARDES_PAIR: `pardes FILE` in a terminal is a session forked off for + it with this terminal its first client (the last step's design, 1.1): + the job the client, the terminal its own; the session its child, in a + session of its own, registered (`ps`: detached, the client listed), + the file drawn, keys in, the digests agreed once quiet; the session's + Exit ends both. With no daemon the client killed outright ends the + session (its last link). Nothing left behind.""" + scratch = Path(__file__).resolve().parent.parent.parent / '.scratch' / 'daemon' / 'dmn' + scratch.mkdir(parents=True, exist_ok=True) + root = Path(tempfile.mkdtemp(prefix='rp-', dir=scratch)) + env = {k: v for k, v in os.environ.items() if not k.startswith('PARDES_') and 'PAGER' not in k} + env.update(HOME=str(root), XDG_RUNTIME_DIR=str(root), XDG_CONFIG_HOME=str(root / 'config'), + TERM='xterm-256color', SHELL=shutil.which('bash') or '/bin/bash', PARDES_NOTIME='1', PARDES_PAIR='1') + (root / 'config' / 'pardes').mkdir(parents=True) + (root / 'config' / 'pardes' / 'init').write_text('Shell bash\nMessageLinger 0\n') + notes = root / 'notes.txt' + notes.write_text(''.join(f'line {i} of the notes\n' for i in range(30))) + terms = [] + + def session_of(name): + sock = root / f'pardes-9p-{name}.sock' + wait_for(f'session {name}\'s 9P socket', sock.exists) + nine = Client(sock) + return nine, int(dict(line.split(maxsplit=1) for line in nine.read('/status').decode().splitlines())['pid']) + + try: + t = Term(30, 100) + terms.append(t) + c = t.spawn([binary, '--9p=pair', str(notes)], dict(env, PARDES_REPLICA_CHECK=str(root / 'pair.check'))) + nine, session = session_of('pair') + assert session != c.pid and ppid(session) == c.pid and os.getsid(session) == session, (session, c.pid) + wait_for('the file drawn on the terminal', lambda: b'line 3 of the notes' in t.text(), timeout=30) + listed = lambda: subprocess.run([binary, 'ps'], env=env, capture_output=True).stdout.decode() + wait_for('the client listed', lambda: f'session pair {session} (tty, detached)' in listed() + and f'client {c.pid} ' in listed()) + pane = next(int(row.split()[0]) for row in nine.read('/index').splitlines() if row.split()[1] == b'text') + os.write(t.master, b'Ofrom-the-pair\x1b') + wait_for('its keys in the session\'s text', lambda: b'from-the-pair' in nine.read(f'/pane/{pane}/body')) + wait_for('out of insert mode', lambda: normal_again(t)) + wait_for('the client agrees once quiet', lambda: agreed(root / 'pair.check', 0)) + exit_session(nine, pane) + code = c.wait(timeout=60) + assert code == 0 and not alive(session), (code, alive(session)) + print(f'pair: client {c.pid} (the job) and session {session} (its child, setsid, listed detached with the client); ' + f'drawn, typed, agreed; Exit ended both (the client exit {code})') + nine.socket.close() + lone = Term(24, 80) + terms.append(lone) + c2 = lone.spawn([binary, '--9p=lone', str(notes)], dict(env, PARDES_DAEMON='off')) + nine2, session2 = session_of('lone') + wait_for('the lone pair drawn', lambda: b'line 3 of the notes' in lone.text(), timeout=30) + nine2.socket.close() + os.kill(c2.pid, signal.SIGKILL) + c2.wait(timeout=10) + wait_for('the lone session ended with its last link', lambda: not alive(session2), timeout=20) + wait_for('nothing left', lambda: not [p for p in processes(root) if alive(p)], timeout=20) + print(f'pair: with no daemon, client {c2.pid} killed outright, its session {session2} ended; nothing left behind') + finally: + for p in processes(root): + try: + os.kill(p, signal.SIGKILL) + except OSError: + pass + for t in terms: + t.close() + shutil.rmtree(root, ignore_errors=True) + + +def pair_rows(binary): + """PARDES_PAIR, the shell's view (the last step's design, 1.1) and a + session's lifetime (2.2 B), one row a step. With a daemon: a Detach in + the pair's client says so and exits 0, the session runs on (listed); an + attach is a client again; TERM to the client puts its terminal back and + the session runs on; so does a KILL, and a closed terminal (HUP): each a + loss, not a close. With none: a Detach refused (nothing could attach), + the client on; its terminal closed, the session ends with its last link.""" + scratch = Path(__file__).resolve().parent.parent.parent / '.scratch' / 'daemon' / 'dmn' + scratch.mkdir(parents=True, exist_ok=True) + root = Path(tempfile.mkdtemp(prefix='rq-', dir=scratch)) + env = {k: v for k, v in os.environ.items() if not k.startswith('PARDES_') and 'PAGER' not in k} + env.update(HOME=str(root), XDG_RUNTIME_DIR=str(root), XDG_CONFIG_HOME=str(root / 'config'), + TERM='xterm-256color', SHELL=shutil.which('bash') or '/bin/bash', PARDES_NOTIME='1', PARDES_PAIR='1') + (root / 'config' / 'pardes').mkdir(parents=True) + (root / 'config' / 'pardes' / 'init').write_text('Shell bash\nMessageLinger 0\n') + notes = root / 'notes.txt' + notes.write_text(''.join(f'line {i} of the notes\n' for i in range(30))) + terms = [] + + def listed(): + return subprocess.run([binary, 'ps'], env=env, capture_output=True).stdout.decode() + + def drawn(t): + wait_for('drawn', lambda: b'line 3 of the notes' in t.text(), timeout=30) + + def typed_detach(t, nine, pane): + # Its view the live one (it typed last), then the word. + os.write(t.master, b'j') + time.sleep(.3) + nine.write(f'/pane/{pane}/exec', b'Detach\n') + + def started(name, e): + t = Term(30, 100) + terms.append(t) + c = t.spawn([binary, f'--9p={name}', str(notes)], e) + wait_for('the socket', (root / f'pardes-9p-{name}.sock').exists) + nine = Client(root / f'pardes-9p-{name}.sock') + session = int(dict(line.split(maxsplit=1) for line in nine.read('/status').decode().splitlines())['pid']) + pane = next(int(row.split()[0]) for row in nine.read('/index').splitlines() if row.split()[1] == b'text') + drawn(t) + return t, c, nine, session, pane + + try: + t, c, nine, session, pane = started('rows', env) + typed_detach(t, nine, pane) + assert c.wait(timeout=20) == 0, c.returncode + wait_for('the detach said', lambda: b"pardes: detached from 'rows', which is still running; come back with `pardes attach rows`" in t.text()) + assert alive(session) and f'session rows {session} (tty, detached)' in listed() + print('pair rows: Detach in the client: said, exit 0; the session runs on, listed') + for how in ('TERM', 'KILL', 'HUP'): + tt = Term(30, 100) + terms.append(tt) + a = tt.spawn([binary, 'attach', 'rows'], env) + drawn(tt) + wait_for('the client listed', lambda: f'client {a.pid} ' in listed()) + if how == 'HUP': + tt.close() + terms.remove(tt) + else: + os.kill(a.pid, getattr(signal, 'SIG' + how)) + a.wait(timeout=20) + if how == 'TERM': + wait_for('its terminal put back', lambda: tt.text().rfind(b'\x1b[?1049l') > tt.text().rfind(b'\x1b[?1049h')) + wait_for('the client unlisted', lambda: f'client {a.pid} ' not in listed()) + assert alive(session), how + print(f'pair rows: a client ended by {how}: the session runs on') + nine.socket.close() + assert subprocess.run([binary, 'kill', 'rows'], env=env, capture_output=True, timeout=30).returncode == 0 + wait_for('the session ended', lambda: not alive(session), timeout=30) + t, c, nine, session, pane = started('lone', dict(env, PARDES_DAEMON='off')) + typed_detach(t, nine, pane) + time.sleep(1) + assert c.poll() is None and alive(session), 'a Detach with no daemon is refused' + nine.socket.close() + t.close() + terms.remove(t) + c.wait(timeout=20) + wait_for('the lone session ended with its last link', lambda: not alive(session), timeout=20) + wait_for('nothing left', lambda: not [p for p in processes(root) if alive(p)], timeout=20) + print('pair rows: no daemon: a Detach refused, the client on; its terminal closed, the session ended; nothing left') + finally: + for p in processes(root): + try: + os.kill(p, signal.SIGKILL) + except OSError: + pass + for t in terms: + t.close() + shutil.rmtree(root, ignore_errors=True) + + def manage(binary): """`pardes --ps`, `--kill=NAME` and `--kill-all`: two detached sessions and their shells listed, one session killed outright and its shell @@ -966,10 +1198,10 @@ def window_clients(binary, gui_binary): wait_for('the window\'s keys after the upgrade', lambda: b'after-upgrade-W' in body(), timeout=30) wait_for('the window client agrees after the upgrade', lambda: agreed(dcheck, seen), timeout=30) assert wd.p.poll() is None - # Detach typed in it: it alone goes, and says why. + # Detach typed in it: it alone goes, and says so (exit 0). wd.keys(b' sD') wait_for('the window client let go', lambda: wd.p.poll() is not None, timeout=15) - assert 'let this client go' in wd.said(), wd.said()[-500:] + assert wd.p.returncode == 0 and "detached from 'files', which is still running" in wd.said(), wd.said()[-500:] # A link a line: the first join sent every blob, its rejoins held them. links = joins(root / 'wd.stats') assert len(links) == 3 and links[0]['blobs'] > 0 and links[0]['from_memory'] + links[0]['from_disk'] == 0, links @@ -1521,6 +1753,9 @@ def main(binary, cycles): clients(binary) far(binary) by_hash(binary) + launch(binary) + pair(binary) + pair_rows(binary) manage(binary) relink(binary) shell = terminals(client)[0] @@ -1557,8 +1792,8 @@ def main(binary, cycles): 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()) + assert c.wait(timeout=10) == 0, c.returncode + wait_for('the client says it detached', lambda: b"detached from 'work', which is still running" in term.text()) else: term.close() c.wait(timeout=10) @@ -1686,8 +1921,8 @@ def main(binary, cycles): # 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()) + assert stub.wait(timeout=10) == 0, stub.returncode + assert b'let this client go' not in term.text(), term.text()[-300:] client.socket.close() term.close() wait_for('the adopted shell hung up', lambda: not alive(shell_pid)) @@ -2231,6 +2466,11 @@ if __name__ == '__main__': clients(sys.argv[2]) elif sys.argv[1:2] == ['--manage']: manage(sys.argv[2]) + elif sys.argv[1:2] == ['--launch']: + launch(sys.argv[2]) + elif sys.argv[1:2] == ['--pair']: + pair(sys.argv[2]) + pair_rows(sys.argv[2]) elif sys.argv[1:2] == ['--relink']: relink(sys.argv[2]) elif sys.argv[1:2] == ['--games']: diff --git a/test/process.zig b/test/process.zig index 8bcbddeb..e2a4f592 100644 --- a/test/process.zig +++ b/test/process.zig @@ -916,7 +916,8 @@ pub const Child = struct { while (environ[i]) |kv| { const entry = std.mem.span(kv); const eq = std.mem.indexOfScalar(u8, entry, '=') orelse entry.len; - if (std.mem.startsWith(u8, entry, "PARDES_") or std.mem.indexOf(u8, entry[0..eq], "PAGER") != null) { + // PARDES_PAIR kept: the suite run as pairs (main.zig `pair`). + if ((std.mem.startsWith(u8, entry, "PARDES_") and !std.mem.startsWith(u8, entry, "PARDES_PAIR=")) or std.mem.indexOf(u8, entry[0..eq], "PAGER") != null) { var name: [256]u8 = undefined; const n = std.fmt.bufPrintSentinel(&name, "{s}", .{entry[0..@min(eq, 255)]}, 0) catch _exit(126); _ = unsetenv(n); |
