From fdf74e199de7d0fc648f912875323f3dc920ffc2 Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Thu, 29 Oct 2026 20:56:03 -0300 Subject: replica clients over a socket: the session streams each its dump and every unit after it (replica.zig `Peers`, a writer and a reader thread a client, units at each step boundary, a client's input stepped there in its own view), and `pardes --attach=:` and `--attach=:` (through `ssh -tt -L`) are a client in the tty host, its keys applied at once by its own replica and drawn by its own process wire.zig version 15: welcome (the client's id and view, the dump's length, the session's UTC offset and digest), dump pieces, units (with the authority's clocks), terminal messages both ways, a check. A unit carries one digest, at its end (`unit_digests`). The client: tty.zig `Shell.remote`, `feed` (a terminal's input to the replica and the session); the session's clock guessed from the last unit's (tape.zig `Guess`); a pump woken only by a client's post reads no new time (`mail_only`), and a unit of another's ending where the last ended leaves the speculation as it is (`neutral`); the rollback copy taken while idle (`Replica.ready`); views a frontend here does not draw left undrawn. A lagging client never holds the session: its queue is cut off past 64 MiB (PARDES_REPLICA_LAG_KB). Detach in a client ends that client; a Restore lets every client go. daemon.zig: a replica the daemon hands over is the session's loop's (a note), not a stub thread's. A window session refuses one in words. test/daemon.py `--clients`: two clients (one through the delay relay) type into a detached session and agree with it, one killed outright, one detached, a third typing into a shell. Co-Authored-By: Claude Opus 5.5 --- test/daemon.py | 153 ++++++++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 147 insertions(+), 6 deletions(-) (limited to 'test/daemon.py') diff --git a/test/daemon.py b/test/daemon.py index ce19b502..b54b2b54 100644 --- a/test/daemon.py +++ b/test/daemon.py @@ -7,8 +7,12 @@ private HOME and runtime directory: - 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 - build handed to its session, which answers it and its pings; another - build's refused, and a session nobody has; + build handed to its session, which welcomes it, sends its dump and + answers its pings; another build's refused, and a session nobody has; +- replica clients (`pardes --attach=:`, phase 5 part B) of a + detached session, one through the delay relay: each one's keys in the + session's text, every client's digest the session's once quiet, one + killed outright while the other types on; - nothing left behind: no session, shell, daemon or socket. python3 -B test/daemon.py [cycles] @@ -17,6 +21,8 @@ private HOME and runtime directory: 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 + python3 -B test/daemon.py --clients + the replica clients alone """ import fcntl import os @@ -170,7 +176,7 @@ def semantics(binary): class Replica: """A replica's connection (src/detached/wire.zig): length-prefixed messages, a tag byte and a little-endian u32 length.""" - WIRE = 14 + WIRE = 15 def __init__(self, path, timeout=10): self.s = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) @@ -194,22 +200,34 @@ class Replica: return tag, self.take(n) def hello(self, session, build): - """('welcome', pid, name) or ('refused', why).""" + """('welcome', pid, name) or ('refused', why); a welcome's dump + is read, whole.""" name = session.encode() self.send(0x01, struct.pack(' after + + +def clients(binary): + """Replica clients of a detached session: one on the replica socket, + one through the delay relay; each one's keys in the session's text and + every client's digest the session's once quiet; one killed outright + while the other types on; 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='rc-', 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') + notes = root / 'notes.txt' + notes.write_text(''.join(f'line {i} of the notes\n' for i in range(300))) + started = [] + terms = [] + try: + sess = subprocess.Popen([binary, '--detach=files', str(notes)], env=env, cwd=root, stdin=subprocess.DEVNULL, + stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL, start_new_session=True) + started.append(sess.pid) + sock = root / 'pardes-replica.sock' + wait_for('the replica socket', sock.exists) + wait_for('the session\'s 9P socket', (root / 'pardes-9p-files.sock').exists) + nine = Client(root / 'pardes-9p-files.sock') + pane = next(int(row.split()[0]) for row in nine.read('/index').splitlines() if row.split()[1] == b'text') + alone = fds(sess.pid) + relay = subprocess.Popen([sys.executable, '-B', str(Path(__file__).parent / 'delay_relay.py'), + str(root / 'relay.sock'), str(sock), '--delay', '40'], + stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL) + started.append(relay.pid) + wait_for('the relay', (root / 'relay.sock').exists) + ta, tb = Term(30, 100), Term(24, 80) + terms += [ta, tb] + a = ta.spawn([binary, f'--attach={sock}:files'], dict(env, PARDES_REPLICA_CHECK=str(root / 'a.check'))) + b = tb.spawn([binary, f'--attach={root / "relay.sock"}:files'], dict(env, PARDES_REPLICA_CHECK=str(root / 'b.check'))) + started += [a.pid, b.pid] + for t in (ta, tb): + wait_for('a client\'s first frame', lambda: b'line 3 of the notes' in t.text(), timeout=20) + os.write(ta.master, b'Ofrom-A\x1b') + os.write(tb.master, b'jjofrom-B\x1b') + wait_for('both clients\' keys in the session\'s text', lambda: b'from-A' in nine.read(f'/pane/{pane}/body') + and b'from-B' in nine.read(f'/pane/{pane}/body')) + 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)) + # Killed outright: the session and the other client carry on. + os.kill(b.pid, signal.SIGKILL) + b.wait(timeout=10) + seen = len(checks(root / 'a.check')) + os.write(ta.master, b'jjjAagain-A\x1b') + wait_for('client A\'s keys after B went', lambda: b'again-A' in nine.read(f'/pane/{pane}/body')) + wait_for('client A agrees again', lambda: agreed(root / 'a.check', seen)) + assert sess.poll() is None + # Detach typed in it: it alone goes. + 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:] + # 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'Tty\n') + shell = wait_for('a shell pane', lambda: next((int(row.split()[0]) for row in nine.read('/index').splitlines() + if row.split()[1] == b'term'), None)) + wait_for('the shell\'s prompt', lambda: b'$' in nine.read(f'/pane/{shell}/body') or b'#' in nine.read(f'/pane/{shell}/body')) + alone = fds(sess.pid) + tc = Term(30, 100) + terms.append(tc) + c = tc.spawn([binary, f'--attach={sock}:files'], dict(env, PARDES_REPLICA_CHECK=str(root / 'c.check'))) + started.append(c.pid) + wait_for('client C\'s first frame', lambda: b'$' in tc.text(), timeout=20) + run(nine, shell, b'echo from-the-session\n') + os.write(tc.master, b'echo from-C\r') + wait_for('client C\'s keys in the shell', lambda: b'from-C' in nine.read(f'/pane/{shell}/body')) + wait_for('client C agrees with a shell\'s output', lambda: agreed(root / 'c.check', 0)) + # Its terminal hung up: it dies, and the session carries on. + c.send_signal(signal.SIGHUP) + c.wait(timeout=10) + relay.kill() + relay.wait() + wait_for('the session\'s fds level', lambda: fds(sess.pid) == alone, timeout=10) + print(f'daemon: two replica clients (one through a 40 ms relay) typed into a detached session; ' + f'digests agreed {len(checks(root / "a.check"))} and {len(checks(root / "b.check"))} times; ' + f'one killed, the other typed on and detached; a third typed into a shell, agreed ' + f'{len(checks(root / "c.check"))} times, and was hung up') + try: + nine.write(f'/pane/{pane}/exec', b'Exit\n') + except OSError as e: + # An edited file asks before it goes. + assert 'Exit again' in str(e), e + nine.write(f'/pane/{pane}/exec', b'Exit\n') + sess.wait(timeout=10) + nine.socket.close() + wait_for('nothing left', lambda: not [p for p in processes(root) if alive(p)], timeout=10) + print('daemon: replica clients\' session ended, nothing left behind') + finally: + for t in terms: + t.close() + for pid in started + processes(root): + try: + os.kill(pid, signal.SIGKILL) + except OSError: + pass + shutil.rmtree(root, ignore_errors=True) + + def terminals(client): return [int(row.split()[0]) for row in client.read('/index').splitlines() if row.split()[1] == b'term'] @@ -290,6 +428,7 @@ def main(binary, cycles): daemon = daemons[0] started.append(daemon) replicas(root, binary, session, daemon) + clients(binary) shell = terminals(client)[0] shell_pid = int(run(client, shell, b'echo $$\n').split(b'\n')[1]) started.append(shell_pid) @@ -856,6 +995,8 @@ if __name__ == '__main__': gui_upgrades(Path(sys.argv[3]).resolve(), Path(sys.argv[4]).resolve()) elif sys.argv[1:2] == ['--upgrade']: upgrades(Path(sys.argv[2]).resolve(), Path(sys.argv[3]).resolve()) + elif sys.argv[1:2] == ['--clients']: + clients(sys.argv[2]) elif sys.argv[1:2] == ['--ping']: ping(sys.argv[2], sys.argv[3], sys.argv[4], int(sys.argv[5]) if len(sys.argv) > 5 else 50) elif '--gui' in sys.argv: -- cgit v1.3