diff options
| author | Gabriel Schneider <[email protected]> | 2026-10-31 06:28:48 -0300 |
|---|---|---|
| committer | Gabriel Schneider <[email protected]> | 2026-10-31 06:28:48 -0300 |
| commit | a479792a1ab2b593642d7f648194bfa23ce01dba (patch) | |
| tree | 96ddee7d879a9084daa07db43b0b7910b072a785 /test | |
| parent | e179cc44fe5060c76a5b00c41e53f11209f5acd9 (diff) | |
| parent | 4c761893e5b3fdae422622cfa02104ab36633045 (diff) | |
| download | pardes-a479792a1ab2b593642d7f648194bfa23ce01dba.tar.gz pardes-a479792a1ab2b593642d7f648194bfa23ce01dba.zip | |
Merge the upstream daemon and replicated client/server refactor
Diffstat (limited to 'test')
39 files changed, 6840 insertions, 500 deletions
diff --git a/test/cross_replay.py b/test/cross_replay.py new file mode 100644 index 00000000..21367ca1 --- /dev/null +++ b/test/cross_replay.py @@ -0,0 +1,145 @@ +#!/usr/bin/env python3 +"""A session's tape recorded by one build of pardes and replayed by another +(docs/next-daemon.md, the remote bugs): a window's build replaying a +terminal's session, a terminal's build a window's, a Linux build a FreeBSD +one's. A replica of another build reads its session's units as this replay +reads the tape, so what the tape carries must mean the same to both. + + python3 -B test/cross_replay.py <recorder> <replayer>... + a session of <recorder> recorded here, replayed by it and by each + replayer (a pardes-gui records in a hidden window) + python3 -B test/cross_replay.py --record <recorder> <dir> + the session alone, its tapes left in <dir> (on the box, say) + python3 -B test/cross_replay.py --replay <replayer> <tape>... + each tape replayed alone + +The session meets the host's failures, whose errors cross by name: a Look +of a fifo (a read the host refuses) and of a directory, a stat and a read +under /os over 9P, a Save into a file as if it were a directory and a +Restore of a dump that is not there (errors the host reports, anyerrors). +It greps and finds, as a script does, which a 9P write runs on the loop: +its reads are a tape site (a replica of a session a script greps in read +no file and diverged). And it holds what a digest must name the same in +every build: a +LocationsConfig, whose origin is a Builtin, a file +pane renamed (H19: no Linux client could join a FreeBSD session so), and +a +Help, which lists the build's words (Tty9p was a Linux build's alone). +""" +import os +import shutil +import signal +import subprocess +import sys +import tempfile +import time +from pathlib import Path + +sys.path.insert(0, str(Path(__file__).parent)) +from daemon import Window, alive, processes, wait_for # noqa: E402 +from ninep import Client # noqa: E402 + + +def record(binary, out): + """One session of `binary`, recording into `out`: its tapes' paths.""" + out = Path(out).resolve() + out.mkdir(parents=True, exist_ok=True) + # A short runtime directory: a socket's path holds 108 bytes at most. + root = Path(tempfile.mkdtemp(prefix='xr-', dir=os.environ.get('TMPDIR', '/tmp'))) + 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'), + XDG_STATE_HOME=str(root / 'state'), XDG_CACHE_HOME=str(root / 'cache'), + TERM='xterm-256color', SHELL=shutil.which('bash') or '/bin/sh') + (root / 'config' / 'pardes').mkdir(parents=True) + (root / 'a.txt').write_text('hello\n') + (root / 'dir').mkdir() + (root / 'dir' / 'b.txt').write_text('in a directory\n') + os.mkfifo(root / 'fifo') + # As test/daemon.py's sessions are: a detached session's message goes at + # once, on the clock (Messages.expireUnattended). + (root / 'config' / 'pardes' / 'init').write_text('MessageLinger 0\n') + stem = Path(binary).name + env['PARDES_RECORD'] = str(out / f'{stem}-') + window = 'gui' in stem + started = [] + win = None + try: + if window: + win = Window(root, 'rec', [binary, '--9p=xr', str(root / 'a.txt')], env) + started.append(win.pid) + else: + started.append(subprocess.Popen([binary, '--detach=xr', str(root / 'a.txt')], env=env, cwd=root, + stdin=subprocess.DEVNULL, stdout=subprocess.DEVNULL, + stderr=subprocess.DEVNULL, start_new_session=True).pid) + sock = root / 'pardes-9p-xr.sock' + wait_for('the session\'s 9P socket', sock.exists, timeout=30) + with Client(sock) as c: + status = dict(line.split(maxsplit=1) for line in c.read('/status').decode().splitlines()) + started.append(int(status['pid'])) + pane = next(int(row.split()[0]) for row in c.read('/index').splitlines() if row.split()[1] == b'text') + for line in (f'Look {root / "fifo"}', f'Look {root / "dir"}', f'Save {root / "a.txt" / "x"}', + f'Restore {root / "nothing.dump"}', 'Grep hello', 'Find txt', 'LocationsConfig', 'Help'): + try: + c.write(f'/pane/{pane}/exec', line.encode() + b'\n') + except Exception: + pass # said as the write's error: a failure is what is wanted + time.sleep(.3) + # A pane a word made (+LocationsConfig: its origin a Builtin, + # which a FreeBSD build numbers otherwise), and a file renamed. + c.write(f'/pane/{pane}/ctl', f'name {root / "renamed.txt"}\n'.encode()) + time.sleep(.3) + c.stat(f'/os{root / "a.txt"}') + assert c.read(f'/os{root / "a.txt"}') == b'hello\n' + for _ in range(2): # the failed Save left the text edited: Exit asks again + try: + c.write(f'/pane/{pane}/exec', b'Exit\n') + except Exception: + pass + if win: + win.close() + wait_for('the session\'s end', lambda: not any(alive(p) for p in started), timeout=20) + finally: + for pid in started + processes(root): + try: + os.kill(pid, signal.SIGKILL) + except OSError: + pass + shutil.rmtree(root, ignore_errors=True) + tapes = sorted(out.glob(f'{stem}-*.tape')) + assert tapes, f'{binary} wrote no tape into {out}' + taped = b''.join(t.read_bytes() for t in tapes) + for name in (b'NotAFile', b'NotDir', b'FileNotFound'): # the fifo's, the Save's, the Restore's + assert name in taped, f'no {name} in the tapes' + return tapes + + +def replay(binary, tape): + """`binary`'s replay of `tape`: its replicas' digests agree. A replica + client never draws its session's frames, and only the replica that + draws none (`headless`) must agree with another frontend's tape: the + two that draw meet the other build's drawing (a window's, a terminal's).""" + env = {k: v for k, v in os.environ.items() if not k.startswith('PARDES_')} + env['PARDES_REPLAY'] = str(tape) + got = subprocess.run([binary], env=env, capture_output=True, timeout=600) + said = got.stderr.decode(errors='replace') + alike = ('gui' in Path(tape).name) == ('gui' in Path(binary).name) + agreed = [line for line in said.splitlines() if line.endswith('digests agree')] + assert ('headless replica' in ''.join(agreed)) and (not alike or (got.returncode == 0 and len(agreed) == 3)), \ + f'{binary} replaying {tape}:\n{said[-3000:]}' + print(f'cross_replay: {Path(binary).name} replayed {Path(tape).name}: ' + + '; '.join(line.split(': ', 1)[1] for line in said.splitlines() if 'replica' in line)) + + +if __name__ == '__main__': + if sys.argv[1:2] == ['--record']: + for t in record(sys.argv[2], sys.argv[3]): + print(t) + elif sys.argv[1:2] == ['--replay']: + for t in sys.argv[3:]: + replay(sys.argv[2], t) + else: + out = Path(tempfile.mkdtemp(prefix='xr-tapes-', dir=os.environ.get('TMPDIR', '/tmp'))) + try: + for t in record(sys.argv[1], out): + for b in sys.argv[1:]: + replay(b, t) + finally: + shutil.rmtree(out, ignore_errors=True) diff --git a/test/daemon.py b/test/daemon.py new file mode 100644 index 00000000..bf575fd7 --- /dev/null +++ b/test/daemon.py @@ -0,0 +1,1732 @@ +#!/usr/bin/env python3 +"""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 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 welcomes it, sends its dump and + answers its pings; another build's refused, and a session nobody has; +- replica clients (`pardes --attach=<socket>:<name>`, 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 <pardes> [cycles] + python3 -B test/daemon.py --gui <pardes-gui> [cycles] (a hidden window; + needs a Wayland display, and a pardes beside it for the refusal) + python3 -B test/daemon.py --ping <socket> <session> <pardes> [n] + a hello of <pardes>'s build and n pings on a replica socket (one + `ssh -L` forwards, say), timed + python3 -B test/daemon.py --clients <pardes> + the replica clients alone + python3 -B test/daemon.py --far <pardes> + the far clients and their blob cache alone + python3 -B test/daemon.py --window-clients <pardes> <pardes-gui> + window replica clients (hidden, offscreen) of a terminal's session, + at 0/30/80/207 ms, and a window's session serving replicas +""" +import fcntl +import os +import select +import shutil +import signal +import socket +import statistics +import struct +import subprocess +import sys +import tempfile +import termios +import threading +import time +from pathlib import Path + +sys.path.insert(0, str(Path(__file__).parent)) +from ninep import Client # noqa: E402 + + +def wait_for(what, check, timeout=10): + deadline = time.monotonic() + timeout + while True: + got = check() + if got: + return got + if time.monotonic() > deadline: + raise TimeoutError(what) + time.sleep(.01) + + +class Term: + """A terminal: a pty whose other end is read and kept, its frame + requests answered (CSI 5 n), as a terminal emulator does.""" + + def __init__(self, rows=24, cols=80): + self.master, self.slave = os.openpty() + fcntl.ioctl(self.slave, termios.TIOCSWINSZ, struct.pack('HHHH', rows, cols, 0, 0)) + self.out = bytearray() + self.lock = threading.Lock() + self.closing = False + self.tail = b'' + self.reader = threading.Thread(target=self.drain, daemon=True) + self.reader.start() + + def drain(self): + # Never blocked in a read when `close` comes: a master closed under a + # read stays open until it returns, so the terminal would not hang + # up, and its fd's number, taken again, would be read here. + while not self.closing: + try: + if not select.select([self.master], [], [], 0.05)[0]: + continue + got = os.read(self.master, 65536) + except OSError: + return + if not got: + return + with self.lock: + self.out += got + # A request split across two reads is answered too. + seen, self.tail = self.tail + got, got[-3:] + if b'\x1b[5n' in seen: + try: + os.write(self.master, b'\x1b[0n') + except OSError: + return + + def spawn(self, argv, env): + slave = self.slave + return subprocess.Popen(argv, env=env, stdin=slave, stdout=slave, stderr=slave, + start_new_session=True, + preexec_fn=lambda: fcntl.ioctl(0, termios.TIOCSCTTY, 0)) + + def text(self): + with self.lock: + return bytes(self.out) + + def close(self): + self.closing = True + self.reader.join(timeout=2) + os.close(self.slave) + os.close(self.master) + + +def run(client, serial, line): + """pty/run: a line in, `exit N` and its output out.""" + fid = client.open(f'/pane/{serial}/pty/run', 2) + try: + client.rpc(118, struct.pack('<IQI', fid, 0, len(line)) + line) + return client.read_fid(fid) + finally: + client.close(fid) + + +# FreeBSD has no /proc: ps and procstat answer instead. +procfs = os.path.isdir('/proc/self') + + +def ps(*args): + return subprocess.run(['ps', *args], capture_output=True).stdout + + +def exit_session(nine, pane): + """Exit, by the session's 9P, again when unsaved text asks first. The + Exit that ends the session may close its link before the write's clunk + is answered: that EOF is the session gone, as asked.""" + for _ in range(2): + try: + nine.write(f'/pane/{pane}/exec', b'Exit\n') + return + except EOFError: + return + except OSError as err: + # An edited file asks before it goes. + assert 'Exit again' in str(err), err + + +def processes(root): + """The pids whose environment is this run's (its runtime directory).""" + found = [] + mark = f'XDG_RUNTIME_DIR={root}'.encode() + if not procfs: # ps -e prints each environment before its command + return [int(row.split()[0]) for row in ps('-axeww', '-o', 'pid=', '-o', 'command=').splitlines() + if mark in row.split()] + for entry in os.listdir('/proc'): + if not entry.isdigit(): + continue + try: + environ = open(f'/proc/{entry}/environ', 'rb').read().split(b'\0') + except OSError: + continue + if mark in environ: + found.append(int(entry)) + return found + + +def cmdline(pid): + if not procfs: + return ps('-ww', '-o', 'args=', '-p', str(pid)).split() + try: + return open(f'/proc/{pid}/cmdline', 'rb').read().split(b'\0') + except OSError: + return [] + + +def alive(pid): + try: + os.kill(pid, 0) + except ProcessLookupError: + return False + if not procfs: + return not ps('-o', 'state=', '-p', str(pid)).startswith(b'Z') + try: + state = open(f'/proc/{pid}/stat').read().rsplit(')', 1)[1].split()[0] + except OSError: + return False + return state != 'Z' + + +def fds(pid): + if not procfs: + return len(subprocess.run(['procstat', '-f', str(pid)], capture_output=True).stdout.splitlines()) - 1 + try: + return len(os.listdir(f'/proc/{pid}/fd')) + except OSError: + return -1 + + +def semantics(binary): + """What a replica's build must share with its session's (--version).""" + out = subprocess.run([binary, '--version'], capture_output=True, check=True).stdout.decode() + return int(next(line.split()[1] for line in out.splitlines() if line.startswith('semantics ')), 16) + + +class Replica: + """A replica's connection (src/detached/wire.zig): length-prefixed + messages, a tag byte and a little-endian u32 length. It is a near + client: its first word after the hello says so.""" + WIRE = 19 + + def __init__(self, path, timeout=10): + self.s = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) + self.s.settimeout(timeout) + self.s.connect(os.fspath(path)) + + def send(self, tag, payload): + self.s.sendall(struct.pack('<BI', tag, len(payload)) + payload) + + def take(self, n): + got = b'' + while len(got) < n: + more = self.s.recv(n - len(got)) + if not more: + raise EOFError('the connection ended') + got += more + return got + + def read(self): + tag, n = struct.unpack('<BI', self.take(5)) + return tag, self.take(n) + + def hello(self, session, build): + """('welcome', pid, name) or ('refused', why); a welcome's dump + is read, whole.""" + name = session.encode() + self.send(0x01, struct.pack('<HQH', self.WIRE, build, len(name)) + name + b'\xff') + self.send(0x06, b'\x00') + tag, body = self.read() + if tag == 0x01: + pid, n = struct.unpack_from('<IH', body) + client, view, dump_len, gmtoff, digest = struct.unpack_from("<HBQiQ", body, 6 + n) + got = 0 + while got < dump_len: + tag, piece = self.read() + assert tag == 0x04, tag + got += len(piece) + self.dump_len = dump_len + return 'welcome', pid, body[6:6 + n].decode() + assert tag == 0x02, (tag, body) + n, = struct.unpack_from('<H', body) + return 'refused', body[2:2 + n].decode() + + def ping(self, value): + """One round trip, in ms (the session's units and terminal + messages on the way are passed over).""" + t = time.monotonic() + self.send(0x03, struct.pack('<Q', value)) + while True: + tag, body = self.read() + if tag not in (0x05, 0x06): + break + assert (tag, body) == (0x03, struct.pack('<Q', value)), (tag, body) + return (time.monotonic() - t) * 1000 + + def close(self): + self.s.close() + + +def replicas(root, binary, session, daemon): + sock = root / 'pardes-replica.sock' + wait_for('the replicas\' socket', sock.exists) + assert len(os.fspath(sock)) < 104, 'FreeBSD\'s sun_path' + ours = semantics(binary) + before = fds(session), fds(daemon) + t = time.monotonic() + r = Replica(sock) + said = r.hello('work', ours) + hello_ms = (time.monotonic() - t) * 1000 + # The session answers, so the connection is its own now: the daemon + # keeps no copy of it. + assert said == ('welcome', session, 'work'), said + rtt = [r.ping(i) for i in range(50)] + assert fds(session) == before[0] + 1 and fds(daemon) == before[1], (before, fds(session), fds(daemon)) + r.close() + wait_for('the replica\'s end, in the session', lambda: fds(session) == before[0]) + said = Replica(sock).hello('work', ours ^ 1) + assert said[0] == 'refused' and 'another build' in said[1] and f'{ours ^ 1:016x}' in said[1], said + said = Replica(sock).hello('nobody', ours) + assert said == ('refused', "no session called 'nobody'"), said + wait_for('the daemon\'s refused connections closed', lambda: fds(daemon) == before[1]) + print(f'daemon: a replica handed to its session (hello {hello_ms:.2f} ms, ping median ' + f'{statistics.median(rtt):.3f} ms); another build, and a session nobody has, refused') + + +def ping(path, session, binary, n): + build = semantics(binary) + t = time.monotonic() + r = Replica(path, timeout=30) + said = r.hello(session, build) + hello_ms = (time.monotonic() - t) * 1000 + if said[0] != 'welcome': + sys.exit(f'refused: {said[1]}') + rtt = sorted(r.ping(i) for i in range(n)) + r.send(0x02, b'') + r.close() + print(f'connect and hello {hello_ms:.2f} ms; {n} pings min/median/p90/max ' + f'{rtt[0]:.2f}/{statistics.median(rtt):.2f}/{rtt[int(n * .9) - 1]:.2f}/{rtt[-1]:.2f} ms') + + +def checks(path): + """A replica client's checks against its session (PARDES_REPLICA_CHECK).""" + try: + return path.read_text().splitlines() + except FileNotFoundError: + return [] + + +def normal_again(term): + """The client's cursor a block again after its beam: the Esc it was sent + ended insert mode. A key written before that can reach it in one read + with the Esc, which is then Alt on that key.""" + t = term.text() + return t.rfind(b'\x1b[0 q') > t.rfind(b'\x1b[6 q') + + +def agreed(path, after): + """A check after the first `after` agreed; one that differed fails.""" + lines = checks(path) + bad = [line for line in lines if line.startswith('differ')] + assert not bad, (path.name, bad) + return len(lines) > after + + +def made_live(term, check): + """A key in a replica client's terminal (`gg`), stepped by its session + (a check after it agreed): its view is the live one, the one /layout + reads and /exec runs in, until the next input. A client asks one check + at a time, and checks come on (each answer wakes its session): the + first after the key may have been asked before it, the second was not.""" + seen = len(checks(check)) + os.write(term.master, b'gg') + wait_for('the client\'s key stepped', lambda: agreed(check, seen + 1), timeout=20) + + +def view_panes(client): + """The panes the live view lays out (/layout's pane serials).""" + return {int(x) for line in client.read('/layout').decode().splitlines() + if line.split()[:1] != ['active'] for x in line.split()[6:]} + + +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('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. + tt = Term(24, 80) + terms.append(tt) + stub = tt.spawn([binary, '--attach=files'], env) + started.append(stub.pid) + wait_for('the attached terminal\'s frame', lambda: b'line 3 of the notes' in tt.text(), timeout=20) + stub.send_signal(signal.SIGHUP) + stub.wait(timeout=10) + # One put off the session's state (PARDES_TEST_DIVERGE, at its + # 100th unit): the next unit's digest finds it, it says so in a line + # and joins again for its view, a pane of its own in it, its keys + # after go into that pane and it agrees. + td = Term(24, 90) + terms.append(td) + d = td.spawn([binary, f'--attach={sock}:files'], dict(env, PARDES_REPLICA_CHECK=str(root / 'd.check'), PARDES_TEST_DIVERGE='100')) + started.append(d.pid) + wait_for('client D\'s first frame', lambda: b'line 3 of the notes' in td.text(), timeout=20) + made_live(td, root / 'd.check') + shown = view_panes(nine) + nine.write('/exec', b'New\n') + mine = wait_for('a pane in client D\'s view', lambda: view_panes(nine) - shown).pop() + # Others' units, replayed on D's core: one after its 100th finds it. + deadline = time.monotonic() + 60 + while b'joined again' not in td.text(): + assert time.monotonic() < deadline, 'client D diverged and joined again' + os.write(ta.master, b'jk') + time.sleep(.2) + assert d.poll() is None + made_live(td, root / 'd.check') + assert mine in view_panes(nine), ('client D in its own view again', mine, nine.read('/layout')) + seen = len(checks(root / 'd.check')) + os.write(td.master, b'Oafter-rejoin-D\x1b') + wait_for('client D\'s keys after its rejoin', lambda: b'after-rejoin-D' in nine.read(f'/pane/{mine}/body')) + wait_for('client D out of insert mode', lambda: normal_again(td)) + wait_for('client D agrees after its rejoin', lambda: agreed(root / 'd.check', seen)) + os.write(td.master, b' sD') + wait_for('client D let go', lambda: d.poll() is not None, timeout=10) + # 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 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. + 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 put off the session\'s state joined again in its own view and agreed {len(checks(root / "d.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') + exit_session(nine, pane) + 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 manage(binary): + """`pardes --ps`, `--kill=NAME` and `--kill-all`: two detached sessions + and their shells listed, one session killed outright and its shell + listed as no one's, the other ended by name with its shell, a name no + session has refused, the kept shell ended by --kill-all, and the daemon + gone after it.""" + scratch = Path(__file__).resolve().parent.parent.parent / '.scratch' / 'daemon' / 'dmn' + scratch.mkdir(parents=True, exist_ok=True) + root = Path(tempfile.mkdtemp(prefix='rm-', 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') + + def cli(*args): + return subprocess.run([binary, *args], env=env, cwd=root, capture_output=True, timeout=30) + + started = [] + try: + sessions, shells = {}, {} + for name in ('one', 'two'): + (root / f'{name}.txt').write_text(f'{name}\n') + started.append(subprocess.Popen([binary, f'--detach={name}', str(root / f'{name}.txt')], env=env, cwd=root, + stdin=subprocess.DEVNULL, stdout=subprocess.DEVNULL, + stderr=subprocess.DEVNULL, start_new_session=True).pid) + sock = root / f'pardes-9p-{name}.sock' + wait_for(f'session {name}\'s 9P socket', sock.exists) + with Client(sock) as c: + status = dict(line.split(maxsplit=1) for line in c.read('/status').decode().splitlines()) + sessions[name] = int(status['pid']) + started.append(sessions[name]) + c.write('/pane/1/exec', b'Tty\n') + wait_for(f'session {name}\'s shell', lambda: terminals(c)) + shell = terminals(c)[0] + wait_for(f'session {name}\'s shell answers', lambda: run(c, shell, b'true\n') == b'exit 0\n') + shells[name] = int(run(c, shell, b'echo $$\n').split(b'\n')[1]) + started.append(shells[name]) + daemon = [p for p in processes(root) if cmdline(p)[1:2] == [b'--daemon']][0] + started.append(daemon) + out = cli('--ps') + assert out.returncode == 0, out + listed = out.stdout.decode() + assert f'pardes daemon {daemon}' in listed, listed + for name in ('one', 'two'): + assert f'session {name} {sessions[name]} (tty, detached)' in listed, listed + assert f'shell {shells[name]}' in listed, listed + os.kill(sessions['two'], signal.SIGKILL) + wait_for('session two gone', lambda: not alive(sessions['two'])) + wait_for('its shell no session\'s', lambda: 'no session holds' in cli('--ps').stdout.decode()) + listed = cli('--ps').stdout.decode() + assert 'session two' not in listed and listed.index(f'shell {shells["two"]}') > listed.index('no session holds'), listed + assert alive(shells['two']) + print('manage: --ps listed two sessions and their shells, then a killed one\'s shell as no one\'s') + refused = cli('--kill=nobody') + assert refused.returncode == 1 and b"no session called 'nobody'" in refused.stderr, refused + killed = cli('--kill=one') + assert killed.returncode == 0, killed + assert not alive(sessions['one']) + wait_for('session one\'s shell hung up with it', lambda: not alive(shells['one'])) + assert alive(shells['two']) and 'session one' not in cli('--ps').stdout.decode() + assert cli('--kill-all').returncode == 0 + wait_for('the kept shell ended', lambda: not alive(shells['two'])) + wait_for('the daemon gone once it keeps nothing', lambda: not alive(daemon)) + none = cli('--ps') + assert none.returncode == 1 and b'no pardes daemon is running' in none.stderr, none + print('manage: --kill=one ended it and its shell, --kill-all the kept shell, a name nobody has refused; the daemon left') + finally: + for pid in started + processes(root): + try: + os.kill(pid, signal.SIGKILL) + except (ProcessLookupError, PermissionError): + pass + shutil.rmtree(root, ignore_errors=True) + + +class Window: + """A pardes-gui in test mode, its window offscreen and hidden, its keys + written to its stdin (the test feed: Escape as `CSI 27 u`, which a + pipe's lone ESC would hold for the next key).""" + + def __init__(self, root, name, argv, env): + (root / name).mkdir() + self.err = root / f'{name}.err' + self.p = subprocess.Popen(argv, env=dict(env, PARDES_TEST='1', PARDES_TEST_CAPTURE_DIR=str(root / name), SDL_VIDEO_DRIVER='offscreen'), + cwd=root, stdin=subprocess.PIPE, stdout=subprocess.DEVNULL, stderr=open(self.err, 'w'), start_new_session=True) + self.pid = self.p.pid + + def keys(self, text): + self.p.stdin.write(text.replace(b'\x1b', b'\x1b[27u')) + self.p.stdin.flush() + + def mouse(self, kind, button, x, y): + """A pointer event at window pixels (gui.zig's test feed).""" + self.p.stdin.write(f'\x1b]777;mouse;{kind};{button};{x};{y}\x07'.encode()) + self.p.stdin.flush() + + def said(self): + return self.err.read_text(errors='replace') if self.err.exists() else '' + + def close(self): + if self.p.poll() is None: + self.p.stdin.close() + self.p.wait(timeout=15) + return self.p.returncode + + +def sessions_listed(binary, env, root): + """`pardes --ps`'s session lines, each `[name, pid, (kind, state)]`.""" + out = subprocess.run([binary, '--ps'], env=env, cwd=root, capture_output=True, timeout=30) + assert out.returncode == 0, out + rows = [line.split(maxsplit=3)[1:] for line in out.stdout.decode().splitlines() if line.lstrip().startswith('session ')] + return [[name, pid, rest] for name, pid, rest in rows] + + +def window_clients(binary, gui_binary): + """Window replica clients (`pardes-gui --attach=<socket>:<name>`): of a + terminal's detached session, directly and through the delay relay at + 30, 80 and 207 ms, beside a terminal's client, one put off the session's + state that joins again, one that detaches; and a window's session served + to a terminal's client and a window's. Each one's keys in the session's + text, every client's digest the session's once quiet; nothing left.""" + scratch = Path(__file__).resolve().parent.parent.parent / '.scratch' / 'daemon' / 'dmn' + scratch.mkdir(parents=True, exist_ok=True) + root = Path(tempfile.mkdtemp(prefix='wc-', dir=os.environ.get('TMPDIR') or 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 = [] + windows = [] + 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') + body = lambda: nine.read(f'/pane/{pane}/body') + # A terminal's client beside the windows, typing as they do. + ta = Term(30, 100) + terms.append(ta) + a = ta.spawn([binary, f'--attach={sock}:files'], dict(env, PARDES_REPLICA_CHECK=str(root / 'a.check'))) + started.append(a.pid) + wait_for('the terminal client\'s first frame', lambda: b'line 3 of the notes' in ta.text(), timeout=30) + agreements = [] + for i, rtt in enumerate((0, 30, 80, 207)): + to = sock + relay = None + if rtt: + relay = subprocess.Popen([sys.executable, '-B', str(Path(__file__).parent / 'delay_relay.py'), + str(root / f'relay{rtt}.sock'), str(sock), '--delay', str(rtt / 2)], + stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL) + started.append(relay.pid) + wait_for('the relay', (root / f'relay{rtt}.sock').exists) + to = root / f'relay{rtt}.sock' + check = root / f'w{rtt}.check' + w = Window(root, f'w{rtt}', [gui_binary, f'--attach={to}:files'], dict(env, PARDES_REPLICA_CHECK=str(check))) + windows.append(w) + started.append(w.pid) + seen = len(checks(root / 'a.check')) + w.keys(b'jjjjjjj' * (i + 1) + f'Ofrom-window-{rtt}\x1b'.encode()) + os.write(ta.master, b'jjjjOfrom-terminal-%d\x1b' % rtt) + wait_for(f'the window\'s and the terminal\'s keys ({rtt} ms)', lambda: f'from-window-{rtt}'.encode() in body() + and b'from-terminal-%d' % rtt in body(), timeout=30) + wait_for('the terminal client normal again', lambda: normal_again(ta), timeout=20) + wait_for(f'the window client agrees ({rtt} ms)', lambda: agreed(check, 0), timeout=30) + wait_for('the terminal client agrees', lambda: agreed(root / 'a.check', seen), timeout=30) + if not rtt: + # Its clients are no sessions of the daemon's. + listed = sessions_listed(binary, env, root) + assert [(n, k) for n, _, k in listed] == [('files', '(tty, detached)')], listed + # Moved and scrolled, its own view only: agreed again. + n = len(checks(check)) + w.keys(b'jjjjjkkk\x06\x06\x02') + wait_for(f'the window client agrees again ({rtt} ms)', lambda: agreed(check, n), timeout=30) + if rtt in (0, 207): + # The pointer over the tags (the workspace's, the column's, + # the pane's: the top rows, a test window's cells about 16 by + # 33 pixels) and over the body, a click there: a window's + # hits are its own frame's, which the session never draws, + # so they are its input's, not re-made from a frame. Each + # tag motion made it join again (the user's, 2026-10-30). + n = len(checks(check)) + for y in range(5, 170, 11): + for x in range(4, 1280, 23): + w.mouse('motion', 0, x, y) + for k in range(40): + w.mouse('motion', 0, 100 + 25 * k, 200 + 9 * k) + w.mouse('down', 1, 400, 300) + w.mouse('motion', 1, 600, 360) + w.mouse('up', 1, 600, 360) + wait_for(f'the window client agrees after its pointer ({rtt} ms)', lambda: agreed(check, n), timeout=30) + assert 'joined again' not in w.said(), w.said()[-800:] + agreements.append(len(checks(check))) + assert w.close() == 0, w.said()[-800:] + if relay: + relay.kill() + relay.wait() + # One put off the session's state (PARDES_TEST_DIVERGE): the next + # unit it replays finds it, it joins again, and its keys after go in. + # Far (PARDES_REPLICA_FAR, as through ssh): its first join sent its + # blobs by hash, in zstd; its rejoin finds them in the core it left. + dcheck = root / 'wd.check' + wd = Window(root, 'wd', [gui_binary, f'--attach={sock}:files'], dict(env, PARDES_REPLICA_CHECK=str(dcheck), PARDES_TEST_DIVERGE='1', + PARDES_REPLICA_FAR='1', PARDES_REPLICA_STATS=str(root / 'wd.stats'), + XDG_CACHE_HOME=str(root / 'wd-cache'))) + windows.append(wd) + started.append(wd.pid) + time.sleep(2) + wd.keys(b'jj') + time.sleep(.5) + os.write(ta.master, b'jj') + wait_for('the window client diverged and joined again', lambda: 'joined again' in wd.said(), timeout=30) + assert wd.p.poll() is None + seen = len(checks(dcheck)) + wd.keys(b'Oafter-rejoin-W\x1b') + wait_for('the window\'s keys after its rejoin', lambda: b'after-rejoin-W' in body(), timeout=30) + wait_for('the window client agrees after its rejoin', lambda: agreed(dcheck, seen), timeout=30) + # An Upgrade (to the same build): the session lets it go in words + # (`refused`), it dials again, joins, and its keys after go in. + nine.write('/ctl', b'Upgrade\n') + wait_for('the window client joined again after the upgrade', lambda: 'joined again: the session is upgrading' in wd.said(), timeout=60) + nine.socket.close() + nine = reconnect(root / 'pardes-9p-files.sock') + seen = len(checks(dcheck)) + wd.keys(b'jjjOafter-upgrade-W\x1b') + 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. + 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:] + # 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 + assert links[1]['from_memory'] == links[1]['blobs'] and links[1]['sent_kb'] == 0, links + os.write(ta.master, b' sD') + wait_for('the terminal client let go', lambda: a.poll() is not None, timeout=10) + print(f'daemon: window clients of a terminal\'s session at 0/30/80/207 ms beside a terminal\'s, digests agreed ' + f'{agreements} and {len(checks(root / "a.check"))} times; one put off the session\'s state joined again, ' + f'joined again across an Upgrade, agreed {len(checks(dcheck))} times and detached (far: its joins {joins(root / "wd.stats")})') + exit_session(nine, pane) + sess.wait(timeout=10) + nine.socket.close() + # A window's session (its daemon on, as a desktop's is), served to a + # terminal's client and a window's, the window typing as they do. + win = Window(root, 'win', [gui_binary, '--9p=win', str(notes)], dict(env, PARDES_DAEMON='on')) + windows.append(win) + started.append(win.pid) + wait_for('the window session\'s 9P socket', (root / 'pardes-9p-win.sock').exists, timeout=30) + wait_for('the replica socket', sock.exists) + nine = Client(root / 'pardes-9p-win.sock') + pane = next(int(row.split()[0]) for row in nine.read('/index').splitlines() if row.split()[1] == b'text') + tb = Term(30, 100) + terms.append(tb) + b = tb.spawn([binary, f'--attach={sock}:win'], dict(env, PARDES_REPLICA_CHECK=str(root / 'b.check'))) + started.append(b.pid) + wait_for('the terminal client\'s first frame', lambda: b'line 3 of the notes' in tb.text(), timeout=30) + wg = Window(root, 'wg', [gui_binary, f'--attach={sock}:win'], dict(env, PARDES_REPLICA_CHECK=str(root / 'g.check'))) + windows.append(wg) + started.append(wg.pid) + time.sleep(2) + os.write(tb.master, b'jjjOfrom-T\x1b') + wg.keys(b'jjjjjjOfrom-G\x1b') + win.keys(b'jjjjjjjjjOfrom-W\x1b') + wait_for('everyone\'s keys in the window\'s session', lambda: all(x in body() for x in (b'from-T', b'from-G', b'from-W')), timeout=30) + wait_for('the terminal client normal again', lambda: normal_again(tb), timeout=20) + for k in range(3): + nb, ng = len(checks(root / 'b.check')), len(checks(root / 'g.check')) + os.write(tb.master, b'jk') + wg.keys(b'kj') + win.keys(b'jj') + wait_for('the terminal client agrees with the window', lambda: agreed(root / 'b.check', nb), timeout=30) + wait_for('the window client agrees with the window', lambda: agreed(root / 'g.check', ng), timeout=30) + listed = sessions_listed(binary, env, root) + assert [(n, k) for n, _, k in listed] == [('win', '(gui, attached)')], listed + assert wg.close() == 0, wg.said()[-800:] + os.write(tb.master, b' sD') + wait_for('the terminal client let go', lambda: b.poll() is not None, timeout=10) + assert win.p.poll() is None + print(f'daemon: a window\'s session served a terminal\'s client and a window\'s, digests agreed ' + f'{len(checks(root / "b.check"))} and {len(checks(root / "g.check"))} times; --ps listed it alone, (gui, attached)') + win.close() + nine.socket.close() + wait_for('nothing left', lambda: not [p for p in processes(root) if alive(p)], timeout=15) + print('daemon: window clients\' sessions ended, nothing left behind') + finally: + for t in terms: + t.close() + for w in windows: + if w.p.poll() is None: + w.p.kill() + for pid in started + processes(root): + try: + os.kill(pid, signal.SIGKILL) + except OSError: + pass + shutil.rmtree(root, ignore_errors=True) + + +def joins(path): + """A replica client's joins (PARDES_REPLICA_STATS, a line a link): how + many blobs each listed, how many it held already from the core it left + and from disk, and the KB it was sent.""" + out = [] + for line in checks(path): + words = line.split() + if 'blobs' not in words: + continue + got = {words[i]: int(words[i + 1]) for i in range(0, len(words) - 1, 2) if words[i + 1].isdigit()} + out.append(got) + return out + + +def cached(cache): + """The blob cache's files (a far client's, XDG_CACHE_HOME/pardes/blobs).""" + d = cache / 'pardes' / 'blobs' + return {p.name: p.stat().st_size for p in d.iterdir() if p.is_file()} if d.exists() else {} + + +def far(binary): + """Far replica clients (PARDES_REPLICA_FAR, as through ssh) of a + detached session and their blob cache: the first one is sent every + blob and keeps them; a second on the same machine, and one put off the + session's state that joins again, are sent none they hold, from disk + and from the core they leave; one started after the first ended gets + what it made from disk; one after the cache was corrupted and cut short + by hand, and one with the cache full, are sent what they lack. Every + client's digest is the session's once quiet.""" + scratch = Path(__file__).resolve().parent.parent.parent / '.scratch' / 'daemon' / 'dmn' + scratch.mkdir(parents=True, exist_ok=True) + root = Path(tempfile.mkdtemp(prefix='rf-', dir=scratch)) + cache = root / 'cache' + 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'), + XDG_CACHE_HOME=str(cache), TERM='xterm-256color', SHELL=shutil.which('bash') or '/bin/bash', + PARDES_NOTIME='1', PARDES_REPLICA_FAR='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))) + big = root / 'big.txt' + big.write_text(''.join(f'{i:06d} the quick brown fox jumps over the lazy dog {i * 7919 % 10007}\n' for i in range(20000))) + started = [] + terms = [] + + def client(name, extra=None): + t = Term(24, 90) + terms.append(t) + c = t.spawn([binary, f'--attach={root / "pardes-replica.sock"}:files'], + dict(env, PARDES_REPLICA_CHECK=str(root / f'{name}.check'), + PARDES_REPLICA_STATS=str(root / f'{name}.stats'), **(extra or {}))) + started.append(c.pid) + wait_for(f'client {name}\'s first frame', lambda: b'line 3 of the notes' in t.text(), timeout=30) + # Its frame's request answered before a key goes: an answer behind + # an Esc, in one read, is taken as Alt with it. + time.sleep(.3) + return t, c + + def texts(): + return b''.join(nine.read(f'/pane/{row.split()[0].decode()}/body') for row in nine.read('/index').splitlines() + if row.split()[1] == b'text') + + def typed(name, t, words): + seen = len(checks(root / f'{name}.check')) + os.write(t.master, b'O' + words) + wait_for(f'client {name}\'s keys in the session\'s text', lambda: words in texts()) + time.sleep(.2) + os.write(t.master, b'\x1b') + wait_for(f'client {name} out of insert mode', lambda: normal_again(t)) + wait_for(f'client {name} agrees once quiet', lambda: agreed(root / f'{name}.check', seen)) + + def detach(name, t, c): + os.write(t.master, b' sD') + wait_for(f'client {name} let go', lambda: c.poll() is not None, timeout=20) + + 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) + wait_for('the replica socket', (root / 'pardes-replica.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') + nine.write(f'/pane/{pane}/exec', f'Look {big}\n'.encode()) + nine.write(f'/pane/{pane}/exec', b'Edit 3a/ an edit the session made /\n') + # A: the first, its cache empty: every blob sent, and kept. + ta, a = client('a') + typed('a', ta, b'from-A') + wait_for('client A\'s blobs on disk', lambda: len(cached(cache)) >= 2) + time.sleep(.5) + # B: a second client on this machine, and put off the session's + # state once quiet: its first join from disk, its rejoin from the + # core it left. + tb, b = client('b', {'PARDES_TEST_DIVERGE': '1'}) + os.write(tb.master, b'jj') + time.sleep(.5) + os.write(ta.master, b'jj') + wait_for('client B diverged and joined again', lambda: b'joined again' in tb.text(), timeout=30) + typed('b', tb, b'from-B') + typed('a', ta, b'again-A') + detach('b', tb, b) + detach('a', ta, a) + a_joins, b_joins = joins(root / 'a.stats'), joins(root / 'b.stats') + assert len(a_joins) == 1 and len(b_joins) == 2, (a_joins, b_joins) + assert a_joins[0]['from_disk'] == 0 and a_joins[0]['from_memory'] == 0 and a_joins[0]['sent_kb'] > 100, a_joins + assert b_joins[0]['from_disk'] >= b_joins[0]['blobs'] - 2 and b_joins[0]['sent_kb'] < a_joins[0]['sent_kb'], b_joins + assert b_joins[1]['from_memory'] >= b_joins[1]['blobs'] - 1 and b_joins[1]['sent_kb'] < 64, b_joins + # C: a client started again after them: what they made since they + # joined is on disk too (Have.keep at their end). + tc, c = client('c') + typed('c', tc, b'from-C') + detach('c', tc, c) + c_join = joins(root / 'c.stats')[0] + assert c_join['from_disk'] >= c_join['blobs'] - 1 and c_join['sent_kb'] < 64, c_join + # D: the cache corrupted and cut short by hand: a miss each, sent again. + files = sorted(cached(cache).items(), key=lambda f: -f[1]) + for k, (name, size) in enumerate(files): + p = cache / 'pardes' / 'blobs' / name + if k % 2: + p.write_bytes(p.read_bytes()[:size // 2]) + else: + data = bytearray(p.read_bytes()) + data[size // 2] ^= 0x55 + p.write_bytes(bytes(data)) + td, d = client('d') + typed('d', td, b'from-D') + detach('d', td, d) + d_join = joins(root / 'd.stats')[0] + assert d_join['from_disk'] == 0 and d_join['sent_kb'] > 100, d_join + # E: the cache full (a 1 MiB cap, held by older files): they go first, + # and the cache stays under its cap. + for k in range(2): + p = cache / 'pardes' / 'blobs' / f'junk-{k}' + p.write_bytes(b'j' * (512 << 10)) + os.utime(p, (1000 + k, 1000 + k)) + te, e = client('e', {'PARDES_BLOB_CACHE_MB': '1'}) + typed('e', te, b'from-E') + detach('e', te, e) + left = cached(cache) + assert not [n for n in left if n.startswith('junk')] and sum(left.values()) <= 1 << 20, left + tf, f = client('f', {'PARDES_BLOB_CACHE_MB': '1'}) + typed('f', tf, b'from-F') + detach('f', tf, f) + f_join = joins(root / 'f.stats')[0] + assert f_join['from_disk'] > 0 and sum(cached(cache).values()) <= 1 << 20, (f_join, cached(cache)) + far_joins(root / 'pardes-replica.sock', 'files', binary, 200) + # Through ssh (a stand-in forwarding as `ssh -L` does): a client + # refused and one detached each end their ssh and its socket (one + # refused left both, 2026-10-30). + fake = root / 'ssh' + fake.write_text('#!/bin/sh\nwhile [ $# -gt 0 ]; do case $1 in -L) fwd=$2; shift;; esac; shift; done\n' + f'exec {sys.executable} -B {Path(__file__).parent / "delay_relay.py"} "${{fwd%%:*}}" "${{fwd#*:}}" --delay 0\n') + fake.chmod(0o755) + senv = dict(env, PARDES_SSH=str(fake), PARDES_REMOTE_RUNTIME=str(root), TMPDIR=str(root)) + + def forwarded(): + return [q for q in processes(root) if alive(q) and any(b'pardes-ssh-' in a for a in cmdline(q))] or list(root.glob('pardes-ssh-*')) + for name in ('nobody', 'files'): + ts = Term(24, 90) + terms.append(ts) + sc = ts.spawn([binary, f'--attach=box:{name}'], senv) + started.append(sc.pid) + if name == 'files': + wait_for('the client through ssh\'s first frame', lambda: b'line 3 of the notes' in ts.text(), timeout=30) + time.sleep(.3) + os.write(ts.master, b' sD') + wait_for(f'the client through ssh ends ({name})', lambda: sc.poll() is not None, timeout=30) + assert name == 'files' or b"no session called 'nobody'" in ts.text(), ts.text()[-300:] + wait_for(f'its ssh and socket gone ({name})', lambda: not forwarded(), timeout=10) + body = texts() + for words in (b'from-A', b'from-B', b'again-A', b'from-C', b'from-D', b'from-E', b'from-F'): + assert words in body, words + print('daemon: far clients: the first sent {} KB; a second from disk ({} of {} blobs), its rejoin from memory ' + '({} of {}); one started again from disk ({} of {}); after the cache was spoilt by hand sent {} KB; with ' + 'the cache full it stayed under its cap; every digest agreed ({} checks)'.format( + a_joins[0]['sent_kb'], b_joins[0]['from_disk'], b_joins[0]['blobs'], b_joins[1]['from_memory'], + b_joins[1]['blobs'], c_join['from_disk'], c_join['blobs'], d_join['sent_kb'], + sum(len(checks(root / f'{n}.check')) for n in 'abcdef'))) + exit_session(nine, pane) + sess.wait(timeout=10) + nine.socket.close() + wait_for('nothing left', lambda: not [p for p in processes(root) if alive(p)], timeout=10) + 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 far_joins(sock, session, binary, n): + """`n` far joins of one session in a row, each read whole (its blobs + asked for by none, its zstd frames unpacked) and answering a ping, then + gone: the session's fds and threads as they were, its RSS within a + bound. Each join made zstd contexts through the C heap, whose release + builds named a new allocator at every malloc and aborted at the 65th + (the box's session, after 15 far joins, 2026-10-30).""" + from compression import zstd + build = semantics(binary) + + def join(): + r = Replica(sock, timeout=30) + name = session.encode() + r.send(0x01, struct.pack('<HQH', Replica.WIRE, build, len(name)) + name + b'\xff') + r.send(0x06, b'\x01') + tag, body = r.read() + assert tag == 0x01, (tag, body) + pid, k = struct.unpack_from('<IH', body) + blobs, = struct.unpack_from('<I', body, len(body) - 4) + if blobs: + r.send(0x07, bytes((blobs + 7) // 8)) + r.send(0x03, struct.pack('<Q', 7)) + d, inner = zstd.ZstdDecompressor(), b'' + while True: + tag, body = r.read() + assert tag == 0x09, tag + inner += d.decompress(body) + while d.eof: + rest, d = d.unused_data, zstd.ZstdDecompressor() + inner += d.decompress(rest) if rest else b'' + while len(inner) >= 5: + t, m = struct.unpack_from('<BI', inner) + if len(inner) < 5 + m: + break + payload, inner = inner[5:5 + m], inner[5 + m:] + if t == 0x03: + assert payload == struct.pack('<Q', 7), payload + r.send(0x02, b'') + r.close() + return pid + + def threads(pid): + return len(os.listdir(f'/proc/{pid}/task')) if procfs else int(ps('-o', 'nlwp=', '-p', str(pid))) + + def rss_kb(pid): + if procfs: + return next(int(line.split()[1]) for line in open(f'/proc/{pid}/status') if line.startswith('VmRSS:')) + return int(ps('-o', 'rss=', '-p', str(pid))) + pid = join() + time.sleep(.5) + before = fds(pid), threads(pid), rss_kb(pid) + t = time.monotonic() + for _ in range(n): + assert join() == pid + wait_for('the far joins\' fds and threads let go', lambda: (fds(pid), threads(pid)) == before[:2], timeout=10) + grown = rss_kb(pid) - before[2] + assert grown < 64 << 10, (before, grown) + print(f'daemon: {n} far joins of one session in {time.monotonic() - t:.1f} s: its fds and threads as they were ' + f'({before[0]}, {before[1]}), its RSS {grown:+d} KB') + + +def terminals(client): + return [int(row.split()[0]) for row in client.read('/index').splitlines() if row.split()[1] == b'term'] + + +def main(binary, cycles): + scratch = Path(__file__).resolve().parent.parent.parent / '.scratch' / 'daemon' / 'dmn' + scratch.mkdir(parents=True, exist_ok=True) + root = Path(tempfile.mkdtemp(prefix='rt-', 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') + daemon_sock = root / 'pardes-daemon.sock' + started = [] + try: + term = Term() + stub = term.spawn([binary, '--tty', '--9p=work'], env) + started.append(stub.pid) + sock = root / 'pardes-9p-work.sock' + wait_for('the session\'s 9P socket', sock.exists) + client = Client(sock) + status = dict(line.split(maxsplit=1) for line in client.read('/status').decode().splitlines()) + session = int(status['pid']) + started.append(session) + assert session != stub.pid, 'the session is forked off its stub' + assert daemon_sock.exists(), list(root.iterdir()) + daemons = [p for p in processes(root) if cmdline(p)[1:2] == [b'--daemon']] + assert len(daemons) == 1, [cmdline(p) for p in processes(root)] + daemon = daemons[0] + started.append(daemon) + replicas(root, binary, session, daemon) + clients(binary) + far(binary) + manage(binary) + shell = terminals(client)[0] + shell_pid = int(run(client, shell, b'echo $$\n').split(b'\n')[1]) + started.append(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) + 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 view of its own + # (src/views.zig), drawn beside the first. Detached from inside, its + # stub ends and the first draws on; closed outright, the same. + 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) + 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') + assert stub2.wait(timeout=10) == 0, stub2.returncode + wait_for('the second stub says it detached', lambda: b'detached from \'work\'' in term2.text()) + term2.close() + else: + term2.close() + stub2.wait(timeout=10) + wait_for('the first terminal\'s view live again', lambda: client.screen()['cols'] == 100) + 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' + 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 drawn beside the first, then detached and closed; the first drew on') + + # 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 + # 20 s and then said only that there was no such session). + tr = Term(24, 90) + rc = tr.spawn([binary, f'--attach={root / "pardes-replica.sock"}:work'], env) + started.append(rc.pid) + wait_for('the replica client\'s first frame', lambda: len(tr.text()) > 2000, timeout=30) + os.kill(session, signal.SIGKILL) + killed = time.monotonic() + wait_for('the replica client ended with its session', lambda: rc.poll() is not None, timeout=10) + assert time.monotonic() - killed < 2 and b'let this client go' in tr.text(), (time.monotonic() - killed, tr.text()[-300:]) + tr.close() + stub.wait(timeout=10) + client.socket.close() + term.close() + wait_for('the killed session gone', lambda: not alive(session)) + time.sleep(.3) + assert alive(shell_pid), 'a killed session\'s shell is not hung up' + assert alive(daemon) + # Started from a shell, which leads the terminal's session and + # outlives the stub, as a terminal's shell does (on FreeBSD the + # leader's exit revokes the terminal for every process). + term = Term() + stub_pid = root / 'stub.pid' + shell = term.spawn(['sh', '-c', '"$@" & echo $! > "$0"; wait; exec sleep 600', + str(stub_pid), binary, '--tty', '--9p=fresh'], env) + started.append(shell.pid) + sock = root / 'pardes-9p-fresh.sock' + wait_for('the second session', sock.exists) + stub = int(stub_pid.read_text()) + started.append(stub) + client = Client(sock) + status = dict(line.split(maxsplit=1) for line in client.read('/status').decode().splitlines()) + session = int(status['pid']) + started.append(session) + # Its stub killed outright, the terminal left open: the session + # (the stub's child) takes that as the terminal's end and detaches. + wait_for('the second session\'s first frame', lambda: b'\x1b[?1049h' in term.text()) + drawn = len(term.text()) + os.kill(stub, signal.SIGKILL) + wait_for('the stub gone', lambda: not alive(stub)) + wait_for('the session detached from its killed stub', lambda: sized(client)) + assert b'\x1b[?1049l' in term.text()[drawn:], 'the terminal is put back' + assert alive(session) + 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() + 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) + client.socket.close() + term.close() + wait_for('the adopted shell hung up', lambda: not alive(shell_pid)) + wait_for('the daemon gone once idle', lambda: not alive(daemon), timeout=5) + left = [p for p in processes(root) if alive(p)] + assert not left, [cmdline(p) for p in left] + sockets = [p.name for p in root.iterdir() if p.is_socket()] + assert not sockets, sockets + print('daemon: nothing left behind') + finally: + for pid in started + processes(root): + try: + os.kill(pid, signal.SIGKILL) + except OSError: + pass + shutil.rmtree(root, ignore_errors=True) + + +def sized(client): + """Whether the session takes a `size`: only with no window or terminal.""" + try: + client.write('/ctl', b'size 100 30\n') + return True + except OSError: + return False + + +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.""" + scratch = Path(__file__).resolve().parent.parent.parent / '.scratch' / 'daemon' / 'dmn' + 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') + (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) + 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, a view of its own (its test + # captures under view-1); Detach in it closes only it. + 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 second window drawn', lambda: (root / 'caps' / 'view-1' / 'latest.ppm').exists()) + client.write(f'/pane/{shell}/exec', b'Detach\n') + time.sleep(.5) + assert not sized(client), 'the first window stays' + assert proc.poll() is None + print('daemon: a second window on an attached window session, detached alone') + # Another kind is refused in words, and so is a name nobody has. + client.write(f'/pane/{shell}/exec', b'Detach\n') + wait_for('the window closed', lambda: sized(client)) + term = Term() + tty_attach = term.spawn([binary.replace('pardes-gui', 'pardes'), '--attach=win'], env) + assert tty_attach.wait(timeout=10) == 1 + wait_for('the refusal in the terminal', lambda: b'is a window' in term.text()) + said = term.text() + term.close() + assert b'is a window' in said, said + nobody = subprocess.run([binary, '--attach=nobody'], env=env, cwd=root, timeout=10, + stdout=subprocess.PIPE, stderr=subprocess.PIPE) + assert nobody.returncode == 1 and b"no session called 'nobody'" in nobody.stderr, nobody.stderr + # Detached and told to end: it ends. + client.write(f'/pane/{shell}/exec', b'Exit\n') + proc.wait(timeout=10) + client.socket.close() + wait_for('the daemon gone once idle', lambda: not [p for p in processes(root) if alive(p)], timeout=5) + print('daemon: window session ended, nothing left behind') + finally: + os.close(feed_w) + for pid in [proc.pid] + processes(root): + try: + os.kill(pid, signal.SIGKILL) + except OSError: + pass + shutil.rmtree(root, ignore_errors=True) + + +def screen_text(client): + shown = client.screen() + cells = ''.join(cell[0] for cell in shown['cells']) + return '\n'.join(cells[i:i + shown['cols']] for i in range(0, len(cells), shown['cols'])) + + +def reconnect(sock, timeout=20): + """The session's 9P again, once its upgraded process listens.""" + return wait_for('the upgraded session\'s 9P', lambda: dial(sock), timeout=timeout) + + +def dial(sock): + try: + return Client(sock) + except (OSError, EOFError): # mid-exec: refused, or cut after the accept + return None + + +def saying(sock, words, timeout=30): + """A connection to the session once its screen shows `words`: dialled + afresh each look, since the process it talks to may exec meanwhile.""" + last = [''] + + def look(): + c = dial(sock) + if c is None: + return None + try: + last[0] = screen_text(c) + if words in last[0]: + return c + except (OSError, EOFError): + pass + c.socket.close() + return None + try: + return wait_for(f'the session saying {words!r}', look, timeout=timeout) + except TimeoutError: + raise TimeoutError(f'the session saying {words!r}; it shows:\n{last[0]}') + + +def exe_of(pid): + """What /proc says the process runs: (its path, whether that file is the + one at the path now). FreeBSD's procstat names the path alone.""" + if not procfs: + rows = subprocess.run(['procstat', '-b', str(pid)], capture_output=True).stdout.decode().splitlines() + return (rows[-1].split()[-1] if len(rows) > 1 else ''), None + link = os.readlink(f'/proc/{pid}/exe') + try: + same = os.stat(f'/proc/{pid}/exe').st_ino == os.stat(link).st_ino + except OSError: + same = False + return link, same + + +def ppid(pid): + if not procfs: + return int(ps('-o', 'ppid=', '-p', str(pid))) + return int(open(f'/proc/{pid}/stat').read().rsplit(')', 1)[1].split()[1]) + + +def install(binary, at): + """As `zig build` and install(1) put a build in place: a new file + renamed over the old, which a process running it keeps, deleted.""" + tmp = at.with_name(at.name + '.new') + if procfs: + subprocess.run(['cp', '--reflink=auto', os.fspath(binary), os.fspath(tmp)], check=True) + else: + shutil.copyfile(binary, tmp) + os.chmod(tmp, 0o755) + os.replace(tmp, at) + + +def upgrades(old, new): + """Upgrade (docs/next-daemon.md, phase 6): a tty session with an unsaved + edit, a shell running `sleep 1000; echo done` and two terminals (two + views) moved onto another build installed at the path it started from, + the daemon with it; then a build that refuses the dump, which falls back + to the running one, and one that refuses it everywhere, which starts + afresh with the shells adopted and the dump kept.""" + scratch = Path(__file__).resolve().parent.parent.parent / '.scratch' / 'daemon' / 'dmn' + scratch.mkdir(parents=True, exist_ok=True) + root = Path(tempfile.mkdtemp(prefix='rtu-', 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 / 'bin').mkdir() + binary = root / 'bin' / 'pardes' + install(old, binary) + notes = root / 'notes.txt' + notes.write_text('one\ntwo\n') + started = [] + try: + term = Term() + stub = term.spawn([str(binary), '--tty', '--9p=work'], env) + started.append(stub.pid) + sock = root / 'pardes-9p-work.sock' + wait_for('the session\'s 9P socket', sock.exists) + client = Client(sock) + session = int(dict(line.split(maxsplit=1) for line in client.read('/status').decode().splitlines())['pid']) + started.append(session) + daemon = wait_for('the daemon', lambda: [p for p in processes(root) if cmdline(p)[1:2] == [b'--daemon']])[0] + started.append(daemon) + shell = terminals(client)[0] + shell_pid = int(run(client, shell, b'echo $$\n').split(b'\n')[1]) + started.append(shell_pid) + client.write(f'/pane/{shell}/look', f'{notes}\n'.encode()) + text = int(client.read('/look')) + client.write(f'/pane/{text}/body', b'unsaved\n') + assert client.read(f'/pane/{text}/body') == b'one\ntwo\nunsaved\n' + client.write(f'/pane/{shell}/pty/data', b'sleep 1000; echo done-after-upgrade\r') + sleeper = wait_for('the sleep', lambda: [p for p in processes(root) if cmdline(p)[:2] == [b'sleep', b'1000']])[0] + # A second terminal: a view of its own. + term2 = Term(30, 100) + stub2 = term2.spawn([str(binary), '--attach=work'], env) + started.append(stub2.pid) + wait_for('the second terminal\'s first frame', lambda: b'\x1b[?1049h' in term2.text()) + old_build = semantics(old) + replica = Replica(root / 'pardes-replica.sock') + assert replica.hello('work', old_build)[0] == 'welcome' + # A replica client of the old build: let go at the upgrade, it dials + # again and is told to upgrade too. + term3 = Term(24, 90) + stale = term3.spawn([str(old), f'--attach={root / "pardes-replica.sock"}:work'], env) + started.append(stale.pid) + wait_for('the old build\'s client drawn', lambda: b'\x1b[?1049h' in term3.text() and b'one' in term3.text()) + before = len(term.text()), len(term2.text()) + + install(new, binary) + assert not procfs or exe_of(session)[1] is False, 'the session runs the old build, deleted' + # Come back to the terminal: the newer build is said. + os.write(term.master, b'\x1b[I') + wait_for('the newer build said', lambda: 'a newer pardes is installed' in screen_text(client)) + fds_before = fds(session), fds(daemon) + rcs = sorted(Path('/tmp').glob(f'pardes-osc133-*-{session}-*')) + t = time.monotonic() + client.write('/ctl', b'Upgrade\n') + client.socket.close() + new_build = semantics(new) + assert new_build != old_build + if procfs: + wait_for('the session on the new build', lambda: exe_of(session) == (str(binary), True), timeout=30) + else: + wait_for('the session registered as the new build', + lambda: Replica(root / 'pardes-replica.sock').hello('work', new_build)[0] == 'welcome', timeout=30) + client = reconnect(sock) + took = time.monotonic() - t + assert int(dict(line.split(maxsplit=1) for line in client.read('/status').decode().splitlines())['pid']) == session + if procfs: + wait_for('the daemon on the new build', lambda: exe_of(daemon) == (str(binary), True)) + else: + wait_for('the daemon exec\'d again', lambda: any(a.startswith(b'--upgraded=') for a in cmdline(daemon))) + assert alive(daemon) and stub.poll() is None and stub2.poll() is None + # A replica of the old build is told why it is let go, after what it + # was sent (units, terminals). + tag, body = replica.read() + while tag in (0x05, 0x06, 0x07): + tag, body = replica.read() + assert tag == 0x02 and b'upgrading to another build' in body, (tag, body) + replica.close() + stale.wait(timeout=30) + assert b'upgrade this client too' in term3.text(), term3.text()[-400:] + term3.close() + print('upgrade: a replica client of the old build was let go, dialled again and was told to upgrade too') + # The old image's prompt files swept, the new one's made. + wait_for('the prompt files level', lambda: len(list(Path('/tmp').glob(f'pardes-osc133-*-{session}-*'))) == len(rcs)) + assert sorted(Path('/tmp').glob(f'pardes-osc133-*-{session}-*')) != rcs or not rcs + # Nothing kept that was not: the old binary let go, every fd back. + # (The replicas' connections are two fewer.) + level = fds_before[0] - 2, fds_before[1] + try: + wait_for('the fds level', lambda: (fds(session), fds(daemon)) == level, timeout=5) + except TimeoutError: + raise AssertionError(('fds', level, (fds(session), fds(daemon)), + os.listdir(f'/proc/{session}/fd') if procfs else None)) + # The edit and its undo. + assert client.read(f'/pane/{text}/body') == b'one\ntwo\nunsaved\n' + assert any(row.split()[0] == str(text).encode() and row.split()[2] == b'1' for row in client.read('/index').splitlines()), client.read('/index') + client.write(f'/pane/{text}/exec', b'Undo\n') + assert client.read(f'/pane/{text}/body') == b'one\ntwo\n', client.read(f'/pane/{text}/body') + # The shell, its job and its pty: the same, the session's still. + assert alive(shell_pid) and ppid(shell_pid) == session and alive(sleeper) + # Both terminals drawn again, each its own view. + wait_for('the first terminal redrawn', lambda: b'\x1b[?1049h' in term.text()[before[0]:]) + wait_for('the second terminal redrawn', lambda: b'\x1b[?1049h' in term2.text()[before[1]:]) + wait_for('the word said', lambda: 'Upgrade: now pardes' in screen_text(client)) + # The daemon lists the session under its new build. + said = Replica(root / 'pardes-replica.sock').hello('work', new_build) + assert said == ('welcome', session, 'work'), said + said = Replica(root / 'pardes-replica.sock').hello('work', old_build) + assert said[0] == 'refused' and 'another build' in said[1], said + os.kill(sleeper, signal.SIGTERM) + wait_for('the shell carrying on', lambda: b'done-after-upgrade\n' in client.read(f'/pane/{shell}/body')) + print(f'upgrade: session {session}, its daemon {daemon}, shell {shell_pid} and two terminals on the new build ' + f'(the word to its 9P again: {took * 1000:.0f} ms); the edit, its undo and the job kept') + + # A build that cannot load the dump: back to the one running (a copy + # of it kept open from /proc/self/exe; FreeBSD keeps none). + refuser = root / 'refuser' + + def refusing(how): + refuser.write_text('#!/bin/sh\n# A pardes whose load refuses the dump (upgrade.zig).\n' + f'case "$1" in --upgraded=*) PARDES_TEST_UPGRADE_REFUSE={how} exec {new} "$@";; esac\n' + f'exec {new} "$@"\n') + refuser.chmod(0o755) + install(refuser, binary) + if procfs: + running = os.stat(f'/proc/{session}/exe').st_ino + # A replica client of the build running: let go at the upgrade, + # it joins again once the session is back, says so, and agrees. + term4 = Term(24, 90) + again = term4.spawn([str(new), f'--attach={root / "pardes-replica.sock"}:work'], + dict(env, PARDES_REPLICA_CHECK=str(root / 'e.check'))) + started.append(again.pid) + wait_for('the client of the build running drawn', lambda: b'\x1b[?1049h' in term4.text() and b'one' in term4.text()) + # A second, and each a pane of its own in its own view: the + # Upgrade's rejoin gives each its own view back, its pane in it, + # not a new one (a copy of the session's, without it). + term5 = Term(30, 100) + again5 = term5.spawn([str(new), f'--attach={root / "pardes-replica.sock"}:work'], + dict(env, PARDES_REPLICA_CHECK=str(root / 'f.check'))) + started.append(again5.pid) + wait_for('the second client of the build running drawn', lambda: b'\x1b[?1049h' in term5.text() and b'one' in term5.text()) + owners = ((term4, root / 'e.check'), (term5, root / 'f.check')) + mine = [] + for t, check in owners: + made_live(t, check) + shown = view_panes(client) + client.write('/exec', b'New\n') + mine.append(wait_for('a pane in the client\'s view', lambda: view_panes(client) - shown).pop()) + refusing('1') + client.write(f'/pane/{shell}/exec', b'Upgrade\n') + client.socket.close() + client = saying(sock, 'it goes on as') + for t, _ in owners: + wait_for('the client joined again', lambda: b'joined again: the session is upgrading' in t.text(), timeout=30) + assert again.poll() is None and again5.poll() is None + for (t, check), pane, theirs in zip(owners, mine, reversed(mine)): + made_live(t, check) + shown = view_panes(client) + assert pane in shown and theirs not in shown, ('its own view again', pane, theirs, client.read('/layout')) + for c, t in ((again, term4), (again5, term5)): + c.send_signal(signal.SIGHUP) + c.wait(timeout=10) + t.close() + print('upgrade: two replica clients of the build running joined again after the upgrade, said so and agreed, ' + 'each in its own view again, its own pane in it') + assert os.stat(f'/proc/{session}/exe').st_ino == running, 'back on the build that ran' + assert alive(shell_pid) and ppid(shell_pid) == session and stub.poll() is None + assert client.read(f'/pane/{text}/body') == b'one\ntwo\n' + assert run(client, shell, b'echo $$\n').split(b'\n')[1] == str(shell_pid).encode() + print('upgrade: a build that refused the dump fell back to the one running; the shell ran on') + + # One that refuses it everywhere: a fresh session, the shells adopted. + refusing('all') + client.write(f'/pane/{shell}/exec', b'Upgrade\n') + client.socket.close() + client = saying(sock, 'brings the rest back') + kept = root / '.local' / 'share' / 'pardes' / f'pardes-upgrade-{session}.dump' + assert kept.exists(), list((root / '.local' / 'share' / 'pardes').iterdir()) + assert alive(shell_pid) and ppid(shell_pid) == session + pids = {p: run(client, p, b'echo $$\n') for p in terminals(client)} + adopted = [p for p, out in pids.items() if out.split(b'\n')[1:2] == [str(shell_pid).encode()]] + assert adopted, ('the shell is a pane of the fresh session', shell_pid, pids, client.read('/index')) + print(f'upgrade: a build that refused it everywhere started afresh, shell {shell_pid} adopted, the dump kept') + + # The daemon, three times upgraded, still holds the shell's master: + # the session killed outright, another adopts it. + install(new, binary) + os.kill(session, signal.SIGKILL) + stub.wait(timeout=10) + stub2.wait(timeout=10) + client.socket.close() + term.close() + term2.close() + wait_for('the killed session gone', lambda: not alive(session)) + time.sleep(.3) + assert alive(shell_pid) and alive(daemon) + term = Term() + stub = term.spawn([str(binary), '--tty', '--9p=again'], env) + started.append(stub.pid) + sock = root / 'pardes-9p-again.sock' + wait_for('the next session', sock.exists) + client = Client(sock) + started.append(int(dict(line.split(maxsplit=1) for line in client.read('/status').decode().splitlines())['pid'])) + # Its reader of the daemon starts after its first frame: an Adopt + # before that waits for an `ok` nobody reads, and is refused. + wait_for('the next session\'s first frame', lambda: b'\x1b[?1049h' in term.text()) + before = set(terminals(client)) + client.write(f'/pane/{min(before)}/exec', b'Adopt\n') + pane = wait_for('the adopted pane', lambda: set(terminals(client)) - before).pop() + assert run(client, pane, b'echo $$\n').split(b'\n')[1] == str(shell_pid).encode() + print(f'upgrade: the upgraded daemon kept shell {shell_pid} through kill -9 of its session, and it was adopted') + client.write(f'/pane/{pane}/exec', b'Exit\n') + stub.wait(timeout=10) + client.socket.close() + term.close() + wait_for('the shell hung up', lambda: not alive(shell_pid)) + wait_for('the daemon gone once idle', lambda: not alive(daemon), timeout=5) + left = [p for p in processes(root) if alive(p)] + assert not left, [cmdline(p) for p in left] + print('upgrade: nothing left behind') + finally: + for pid in started + processes(root): + try: + os.kill(pid, signal.SIGKILL) + except OSError: + pass + shutil.rmtree(root, ignore_errors=True) + + +def gui_upgrades(old, new): + """Upgrade in a window session (test mode: hidden windows, captured): + two windows, an unsaved edit and a shell's job, onto another build.""" + scratch = Path(__file__).resolve().parent.parent.parent / '.scratch' / 'daemon' / 'dmn' + root = Path(tempfile.mkdtemp(prefix='rtgu-', 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 + caps = root / 'caps' + 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(caps), + SDL_VIDEODRIVER='wayland', WAYLAND_DISPLAY=str(display), PARDES_DAEMON='on') + (root / 'config' / 'pardes').mkdir(parents=True) + (root / 'config' / 'pardes' / 'init').write_text('Shell bash\nMessageLinger 0\n') + (root / 'bin').mkdir() + binary = root / 'bin' / 'pardes-gui' + install(old, binary) + notes = root / 'notes.txt' + notes.write_text('one\ntwo\n') + feed_r, feed_w = os.pipe() + proc = subprocess.Popen([str(binary), '--9p=win'], env=env, cwd=root, stdin=feed_r, + stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL, start_new_session=True) + os.close(feed_r) + started = [proc.pid] + try: + sock = root / 'pardes-9p-win.sock' + wait_for('the window session', sock.exists, timeout=20) + client = Client(sock) + daemon = wait_for('the daemon', lambda: [p for p in processes(root) if cmdline(p)[1:2] == [b'--daemon']])[0] + started.append(daemon) + shell = terminals(client)[0] + shell_pid = int(run(client, shell, b'echo $$\n').split(b'\n')[1]) + started.append(shell_pid) + client.write(f'/pane/{shell}/look', f'{notes}\n'.encode()) + text = int(client.read('/look')) + client.write(f'/pane/{text}/body', b'unsaved\n') + client.write(f'/pane/{shell}/pty/data', b'sleep 1000; echo done-after-upgrade\r') + sleeper = wait_for('the sleep', lambda: [p for p in processes(root) if cmdline(p)[:2] == [b'sleep', b'1000']])[0] + attach = subprocess.run([str(binary), '--attach=win'], env=env, cwd=root, timeout=10, + stdout=subprocess.PIPE, stderr=subprocess.PIPE) + assert attach.returncode == 0, attach.stderr + frames = [caps / 'latest.ppm', caps / 'view-1' / 'latest.ppm'] + wait_for('both windows drawn', lambda: all(f.exists() for f in frames)) + install(new, binary) + time.sleep(.05) + mark = time.time() + client.write(f'/pane/{shell}/exec', b'Upgrade\n') + client.socket.close() + wait_for('the session on the new build', lambda: exe_of(proc.pid) == (str(binary), True), timeout=30) + client = reconnect(sock) + assert proc.poll() is None + wait_for('the daemon on the new build', lambda: exe_of(daemon) == (str(binary), True)) + assert client.read(f'/pane/{text}/body') == b'one\ntwo\nunsaved\n' + client.write(f'/pane/{text}/exec', b'Undo\n') + assert client.read(f'/pane/{text}/body') == b'one\ntwo\n' + assert alive(shell_pid) and ppid(shell_pid) == proc.pid and alive(sleeper) + wait_for('both windows drawn again', lambda: all(f.stat().st_mtime > mark for f in frames)) + said = Replica(root / 'pardes-replica.sock').hello('win', semantics(new)) + assert said == ('welcome', proc.pid, 'win'), said + os.kill(sleeper, signal.SIGTERM) + wait_for('the shell carrying on', lambda: b'done-after-upgrade\n' in client.read(f'/pane/{shell}/body')) + print(f'upgrade: window session {proc.pid}, its daemon, shell {shell_pid} and two windows on the new build; ' + 'the edit, its undo and the job kept') + client.write(f'/pane/{shell}/exec', b'Exit\n') + proc.wait(timeout=10) + client.socket.close() + wait_for('nothing left', lambda: not [p for p in processes(root) if alive(p)], timeout=5) + print('upgrade: window session ended, nothing left behind') + finally: + os.close(feed_w) + for pid in started + processes(root): + try: + os.kill(pid, signal.SIGKILL) + except OSError: + pass + shutil.rmtree(root, ignore_errors=True) + + +if __name__ == '__main__': + if sys.argv[1:3] == ['--upgrade', '--gui']: + 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] == ['--manage']: + manage(sys.argv[2]) + elif sys.argv[1:2] == ['--far']: + far(sys.argv[2]) + elif sys.argv[1:2] == ['--window-clients']: + window_clients(sys.argv[2], sys.argv[3]) + 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: + args = [a for a in sys.argv[1:] if a != '--gui'] + gui(args[0], int(args[1]) if len(args) > 1 else 50) + else: + main(sys.argv[1], int(sys.argv[2]) if len(sys.argv) > 2 else 50) diff --git a/test/delay_relay.py b/test/delay_relay.py new file mode 100644 index 00000000..c600a3aa --- /dev/null +++ b/test/delay_relay.py @@ -0,0 +1,274 @@ +#!/usr/bin/env python3 +"""A link with latency, for the replica gates (docs/next-daemon.md, phase 5): +it accepts on one socket, connects each client on to another, and holds +every chunk `delay` ms each way, timed from its arrival, so a stream's +throughput is not serialised behind the delay and its bytes stay in order. +An optional rate cap makes the link a bottleneck too: a chunk leaves once +the ones before it have crossed at that rate, then waits out the delay. An +optional jitter adds up to that many ms more to each chunk's delay, at +random, its order kept (a chunk never overtakes the one before it). + + python3 -B test/delay_relay.py LISTEN TARGET --delay MS [--rate MB/s] [--jitter MS] + python3 -B test/delay_relay.py --self-check + +An address is a path (a Unix socket) or host:port (TCP). The one-way +delays the gates use are 15, 40 and 103.5 ms: RTTs of 30, 80 and 207 ms +(the FreeBSD box's). The self-check measures the RTT and throughput it +adds at each, and with a cap. +""" +import argparse +import os +import queue +import random +import socket +import statistics +import sys +import tempfile +import threading +import time + +CHUNK = 256 << 10 + + +def parse(address): + host, sep, port = address.rpartition(':') + if sep and port.isdigit() and '/' not in address: + return socket.AF_INET6 if ':' in host else socket.AF_INET, (host or '127.0.0.1', int(port)) + return socket.AF_UNIX, address + + +def dial(address): + family, addr = parse(address) + s = socket.socket(family) + s.connect(addr) + if family != socket.AF_UNIX: + s.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1) + return s + + +def listen(address): + family, addr = parse(address) + s = socket.socket(family) + if family == socket.AF_UNIX: + try: + os.unlink(addr) + except FileNotFoundError: + pass + else: + s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) + s.bind(addr) + s.listen(64) + return s + + +def pump(src, dst, delay, rate, done, jitter=0.0): + """src -> dst, each chunk due `delay` s after it arrived (and after the + ones before it crossed at `rate` bytes/s), plus up to `jitter` s at + random but never before the chunk ahead of it; its end a shutdown.""" + due_q = queue.SimpleQueue() + + def reader(): + free = 0.0 # when the capped link has sent what it holds + last = 0.0 # when the chunk before is due: order is kept + while True: + try: + data = src.recv(CHUNK) + except OSError: + data = b'' + now = time.monotonic() + if rate: + free = max(free, now) + len(data) / rate + now = free + last = max(last, now + delay + (random.uniform(0, jitter) if jitter else 0)) + due_q.put((last, data)) + if not data: + return + + def writer(): + try: + while True: + due, data = due_q.get() + wait = due - time.monotonic() + if wait > 0: + time.sleep(wait) + if not data: + dst.shutdown(socket.SHUT_WR) + return + dst.sendall(data) + except OSError: + pass + finally: + done() + + threading.Thread(target=reader, daemon=True).start() + threading.Thread(target=writer, daemon=True).start() + + +def serve(listener, target, delay_ms, rate_mbs=None, jitter_ms=0): + """Relays every connection `listener` accepts until it is closed.""" + delay = delay_ms / 1000 + jitter = jitter_ms / 1000 + rate = rate_mbs * 1e6 if rate_mbs else None + while True: + try: + client, _ = listener.accept() + except OSError: + return + try: + upstream = dial(target) + except OSError: + client.close() + continue + if client.family != socket.AF_UNIX: + client.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1) + left = [2] + lock = threading.Lock() + + def done(a=client, b=upstream, left=left, lock=lock): + with lock: + left[0] -= 1 + if left[0] == 0: + a.close() + b.close() + + pump(client, upstream, delay, rate, done, jitter) + pump(upstream, client, delay, rate, done, jitter) + + +# ---- the self-check ---- + +def echo_server(listener): + def one(c): + with c: + while data := c.recv(CHUNK): + c.sendall(data) + + def loop(): + while True: + try: + c, _ = listener.accept() + except OSError: + return + threading.Thread(target=one, args=(c,), daemon=True).start() + threading.Thread(target=loop, daemon=True).start() + + +def sink_server(listener): + """Reads to the end, then answers one byte: a transfer's end to end.""" + def one(c): + with c: + while c.recv(CHUNK): + pass + c.sendall(b'.') + + def loop(): + while True: + try: + c, _ = listener.accept() + except OSError: + return + threading.Thread(target=one, args=(c,), daemon=True).start() + threading.Thread(target=loop, daemon=True).start() + + +def rtts(address, n=40): + s = dial(address) + out = [] + with s: + for i in range(n): + t = time.monotonic() + s.sendall(i.to_bytes(8, 'little')) + got = b'' + while len(got) < 8: + got += s.recv(8 - len(got)) + out.append((time.monotonic() - t) * 1000) + return out + + +def transfer(address, size): + """Seconds from the first byte sent to the sink's answer.""" + s = dial(address) + block = b'x' * CHUNK + with s: + t = time.monotonic() + left = size + while left > 0: + s.sendall(block[:min(left, CHUNK)]) + left -= CHUNK + s.shutdown(socket.SHUT_WR) + assert s.recv(1) == b'.' + return time.monotonic() - t + + +def self_check(): + tmp = tempfile.mkdtemp(prefix='relay-', dir=os.environ.get('TMPDIR')) + echo, sink = f'{tmp}/echo.sock', f'{tmp}/sink.sock' + echo_server(listen(echo)) + sink_server(listen(sink)) + ok = True + base = statistics.median(rtts(echo)) + size = 256 << 20 + raw = size / transfer(sink, size) / 1e6 + print(f'direct: rtt median {base:.3f} ms, throughput {raw:.0f} MB/s') + for i, delay in enumerate([15, 40, 103.5]): + relays = [] + for name, target in (('e', echo), ('s', sink)): + at = f'{tmp}/{name}{i}.sock' + listener = listen(at) + relays.append(listener) + threading.Thread(target=serve, args=(listener, target, delay), daemon=True).start() + got = rtts(f'{tmp}/e{i}.sock') + lo, med, hi = min(got), statistics.median(got), max(got) + added = med - base + elapsed = transfer(f'{tmp}/s{i}.sock', size) + mbs = size / (elapsed - 2 * delay / 1000) / 1e6 + # The RTT is two delays (scheduling adds well under a millisecond), + # and the delay costs a transfer its two delays, never a delay a + # chunk: serialised, 256 MiB in 256 KiB chunks would take 1024 delays. + good = 2 * delay - 0.05 <= lo and added <= 2 * delay + 1.5 and mbs >= min(200, raw / 4) + ok &= good + print(f'delay {delay} ms each way: rtt min/median/max {lo:.2f}/{med:.2f}/{hi:.2f} ms ' + f'(adds {added:.2f}, want {2 * delay}), throughput {mbs:.0f} MB/s ' + f'({elapsed:.3f} s for {size >> 20} MiB) {"ok" if good else "FAIL"}') + for listener in relays: + listener.close() + # A capped link: the rate holds, and the delay comes on top. + cap, delay, size = 20.0, 40, 40 << 20 + at = f'{tmp}/cap.sock' + listener = listen(at) + threading.Thread(target=serve, args=(listener, sink, delay, cap), daemon=True).start() + elapsed = transfer(at, size) + mbs = size / (elapsed - 2 * delay / 1000) / 1e6 + good = abs(mbs - cap) / cap < 0.05 + ok &= good + print(f'cap {cap} MB/s, delay {delay} ms: {mbs:.2f} MB/s ({elapsed:.3f} s for {size >> 20} MiB) ' + f'{"ok" if good else "FAIL"}') + listener.close() + for f in os.listdir(tmp): + os.unlink(f'{tmp}/{f}') + os.rmdir(tmp) + print('self-check', 'ok' if ok else 'FAILED') + return 0 if ok else 1 + + +def main(): + if '--self-check' in sys.argv: + sys.exit(self_check()) + ap = argparse.ArgumentParser(description=__doc__.split('\n\n')[0]) + ap.add_argument('listen') + ap.add_argument('target') + ap.add_argument('--delay', type=float, required=True, help='one-way delay, ms') + ap.add_argument('--rate', type=float, help='a bandwidth cap, MB/s') + ap.add_argument('--jitter', type=float, default=0, help='up to this many ms more a chunk, at random') + a = ap.parse_args() + listener = listen(a.listen) + print(f'relaying {a.listen} -> {a.target}, {a.delay} ms each way' + + (f', {a.rate} MB/s' if a.rate else '') + (f', jitter {a.jitter} ms' if a.jitter else ''), flush=True) + try: + serve(listener, a.target, a.delay, a.rate, a.jitter) + except KeyboardInterrupt: + pass + + +if __name__ == '__main__': + main() diff --git a/test/e2e_harness.zig b/test/e2e_harness.zig index a2010c19..6011ba16 100644 --- a/test/e2e_harness.zig +++ b/test/e2e_harness.zig @@ -12,9 +12,18 @@ extern "c" fn forkpty( termp: ?*const anyopaque, winp: ?*const posix.winsize, ) c_int; +extern "c" fn openpty( + amaster: *c_int, + aslave: *c_int, + name: ?[*]u8, + termp: ?*const anyopaque, + winp: ?*const posix.winsize, +) c_int; +extern "c" fn ttyname(fd: c_int) ?[*:0]const u8; extern "c" fn execvp(file: [*:0]const u8, argv: [*:null]const ?[*:0]const u8) c_int; extern "c" fn _exit(status: c_int) noreturn; extern "c" fn unsetenv(name: [*:0]const u8) c_int; +extern "c" fn setenv(name: [*:0]const u8, value: [*:0]const u8, overwrite: c_int) c_int; /// Monotonic milliseconds; the harness's only clock (std.time lost /// milliTimestamp in 0.16). @@ -71,6 +80,12 @@ pub const Harness = struct { /// clipboard writes). Capture is bounded explicitly so a runaway child cannot /// consume unbounded test-runner memory. raw: *RawCapture, + /// Another view's terminal of the app this harness started (`view`): + /// it neither started the app nor kills it. + view: bool = false, + /// Every terminal of the app's, this one's too: each wait reads them + /// all, or the app would block writing a frame to one nobody reads. + views: []const *Harness = &.{}, pub fn init(gpa: std.mem.Allocator, exe: [*:0]const u8, rows: u16, cols: u16) !Harness { return initArgs(gpa, exe, rows, cols, null); @@ -96,6 +111,9 @@ pub const Harness = struct { if (pid == 0) { for ([_][*:0]const u8{ "PARDES_PID", "PARDES_9P", "PARDES_PANE" }) |name| if (unsetenv(name) != 0) _exit(126); + // The app is the process this kills and the terminal it hangs + // up (deinit): no daemon to fork it off and keep it (daemon.zig). + if (setenv("PARDES_DAEMON", "off", 1) != 0) _exit(126); _ = execvp(exe, argv.ptr); _exit(127); } @@ -125,8 +143,59 @@ pub const Harness = struct { }; } + /// A terminal for another view of the app `pid` (views.zig): a pty of + /// its own, whose path the app is given (PARDES_TEST_VIEWS) and opens. + pub fn initView(gpa: std.mem.Allocator, rows: u16, cols: u16, path: *[64]u8) !Harness { + var master: c_int = undefined; + var slave: c_int = undefined; + const ws = posix.winsize{ .row = rows, .col = cols, .xpixel = 0, .ypixel = 0 }; + if (openpty(&master, &slave, null, null, &ws) != 0) return error.NoPty; + defer _ = libc.close(slave); + const name = std.mem.span(ttyname(slave) orelse return error.NoPty); + if (name.len >= path.len) return error.NoPty; + @memcpy(path[0..name.len], name); + path[name.len] = 0; + const term = try gpa.create(ghostty_vt.Terminal); + errdefer gpa.destroy(term); + term.* = try ghostty_vt.Terminal.init(std.Io.Threaded.global_single_threaded.io(), gpa, .{ + .cols = cols, + .rows = rows, + .max_scrollback_bytes = 1024 * 1024, + }); + errdefer term.deinit(gpa); + const raw = try gpa.create(RawCapture); + raw.* = .{}; + return .{ .gpa = gpa, .master = master, .pid = 0, .term = term, .stream = term.vtStream(), .rows = rows, .cols = cols, .raw = raw, .view = true }; + } + + /// What the app wrote to its other terminals, read without waiting. + fn drainOthers(self: *Harness) void { + for (self.views) |other| if (other != self) while (other.pumpRead(0)) {}; + } + + fn pumpRead(self: *Harness, ms: i64) bool { + var fds = [_]posix.pollfd{.{ .fd = self.master, .events = posix.POLL.IN, .revents = 0 }}; + const ready = posix.poll(&fds, @intCast(@max(0, ms))) catch 0; + if (ready == 0 or (fds[0].revents & posix.POLL.IN) == 0) return false; + var buf: [4096]u8 = undefined; + const n = posix.read(self.master, &buf) catch return false; + if (n == 0) return false; + self.recordRaw(buf[0..n]); + self.feed(buf[0..n]); + return true; + } + + /// Bytes in every terminal of the app's (`pending`). + pub fn pendingAll(self: *Harness) usize { + var n = self.pending(); + for (self.views) |other| if (other != self) { + n += other.pending(); + }; + return n; + } + pub fn deinit(self: *Harness) void { - posix.kill(self.pid, posix.SIG.KILL) catch {}; + if (!self.view) posix.kill(self.pid, posix.SIG.KILL) catch {}; _ = libc.close(self.master); self.stream.deinit(); self.gpa.destroy(self.raw); @@ -171,8 +240,9 @@ pub const Harness = struct { const deadline = nowMs() + ms; var buf: [4096]u8 = undefined; while (nowMs() < deadline) { + self.drainOthers(); var fds = [_]posix.pollfd{.{ .fd = self.master, .events = posix.POLL.IN, .revents = 0 }}; - _ = posix.poll(&fds, 50) catch {}; + _ = posix.poll(&fds, if (self.views.len > 1) 5 else 50) catch {}; if ((fds[0].revents & posix.POLL.IN) != 0) { const n = posix.read(self.master, &buf) catch break; if (n == 0) break; @@ -189,15 +259,14 @@ pub const Harness = struct { /// SETTLED grid sleeps in poll until the app actually writes, instead of /// burning a fixed window per check (see snapshot.zig's waitStable). pub fn pumpOnce(self: *Harness, ms: i64) !bool { - var fds = [_]posix.pollfd{.{ .fd = self.master, .events = posix.POLL.IN, .revents = 0 }}; - const ready = posix.poll(&fds, @intCast(@max(0, ms))) catch 0; - if (ready == 0 or (fds[0].revents & posix.POLL.IN) == 0) return false; - var buf: [4096]u8 = undefined; - const n = posix.read(self.master, &buf) catch return false; - if (n == 0) return false; - self.recordRaw(buf[0..n]); - self.feed(buf[0..n]); - return true; + if (self.views.len <= 1) return self.pumpRead(ms); + // Several terminals: each looked at every few milliseconds. + const deadline = nowMs() + ms; + while (true) { + self.drainOthers(); + if (self.pumpRead(@min(5, @max(0, deadline - nowMs())))) return true; + if (nowMs() >= deadline) return false; + } } /// Bytes sitting in the pty that nobody has picked up — keystrokes the app @@ -257,8 +326,9 @@ pub const Harness = struct { const deadline = nowMs() + ms; var buf: [4096]u8 = undefined; while (nowMs() < deadline) { + self.drainOthers(); var fds = [_]posix.pollfd{.{ .fd = self.master, .events = posix.POLL.IN, .revents = 0 }}; - _ = posix.poll(&fds, 50) catch {}; + _ = posix.poll(&fds, if (self.views.len > 1) 5 else 50) catch {}; if ((fds[0].revents & posix.POLL.IN) != 0) { const n = posix.read(self.master, &buf) catch break; if (n == 0) break; diff --git a/test/fork_stall.py b/test/fork_stall.py new file mode 100644 index 00000000..c865b6bc --- /dev/null +++ b/test/fork_stall.py @@ -0,0 +1,76 @@ +#!/usr/bin/env python3 +"""What a fork stalls the authority for (docs/next-daemon.md, open point 5: +the join writer as a fork child): a process holding N MB of written +anonymous memory, as a session's heap and arenas are, forks a child that +waits and exits; timed are the fork() itself and the copy-on-write of the +authority's next 64 MB written while the child lives. + + python3 -B test/fork_stall.py [MB ...] (default 100 600 1024 4096) + +On Linux each size runs twice: with transparent huge pages as the system +has them, and with MADV_NOHUGEPAGE (4 KiB pages, as a fragmented heap). +""" +import mmap +import os +import statistics +import sys +import time + +REPS = 9 +COW = 64 << 20 + + +def fill(m, size): + block = b'\1' * (16 << 20) + for at in range(0, size, len(block)): + m[at:at + len(block)] = block[:size - at] + + +def one(size, huge): + m = mmap.mmap(-1, size, flags=mmap.MAP_PRIVATE | mmap.MAP_ANONYMOUS) + if not huge: + m.madvise(mmap.MADV_NOHUGEPAGE) + fill(m, size) + forks, cows = [], [] + for _ in range(REPS): + r, w = os.pipe() + t0 = time.perf_counter_ns() + pid = os.fork() + if pid == 0: + os.read(r, 1) + os._exit(0) + t1 = time.perf_counter_ns() + # What the authority writes next, the child holding its pages. + for at in range(0, min(COW, size), 4096): + m[at] = 2 + t2 = time.perf_counter_ns() + os.write(w, b'x') + os.waitpid(pid, 0) + os.close(r) + os.close(w) + forks.append((t1 - t0) / 1e6) + cows.append((t2 - t1) / 1e6) + # The same writes with no child: the loop's own cost, taken off. + t0 = time.perf_counter_ns() + for at in range(0, min(COW, size), 4096): + m[at] = 3 + loop = (time.perf_counter_ns() - t0) / 1e6 + m.close() + return statistics.median(forks), max(forks), statistics.median(cows) - loop + + +def main(): + sizes = [int(a) for a in sys.argv[1:]] or [100, 600, 1024, 4096] + modes = [True, False] if hasattr(mmap, 'MADV_NOHUGEPAGE') else [True] + print(f'{os.uname().sysname} {os.uname().release}: fork of a process with N MB written, ' + f'median and max of {REPS}; then {COW >> 20} MB written under the child') + for size in sizes: + for huge in modes: + fork_med, fork_max, cow = one(size << 20, huge) + pages = 'system pages' if huge else '4 KiB pages' + print(f'{size:>6} MB {pages:<12} fork {fork_med:8.3f} ms (max {fork_max:8.3f}) ' + f'cow {cow:8.3f} ms', flush=True) + + +if __name__ == '__main__': + main() @@ -22,8 +22,10 @@ from ninep import Client @contextlib.contextmanager def session(binary, root, name, *options, socket_name=None, file=None, tty=False, network=None, launch=None, inherited=None, terminal=None): + # No pager of the caller's (a session's own PAGER, SYSTEMD_PAGER...): + # its shells get the session's defaults, the ones under test. env = {key: value for key, value in os.environ.items() - if not key.startswith('PARDES_')} + if not key.startswith('PARDES_') and 'PAGER' not in key} env.update(HOME=str(root), XDG_RUNTIME_DIR=str(root), XDG_CONFIG_HOME=str(root / 'config'), PARDES_NOTIME='1', PARDES_DUMP=str(root / (name + '.dump.zon'))) @@ -47,6 +49,7 @@ def session(binary, root, name, *options, socket_name=None, file=None, tty=False preexec = lambda: fcntl.ioctl(0, termios.TIOCSCTTY, 0) if launch is not None: args = [binary, *launch] + existing = set(root.glob('pardes-9p-*.sock')) child = subprocess.Popen(args, cwd=root, env=env, stdin=slave, stdout=slave if tty else subprocess.DEVNULL, stderr=subprocess.PIPE, start_new_session=True, @@ -55,7 +58,9 @@ def session(binary, root, name, *options, socket_name=None, file=None, tty=False # Borrowed handles for input-driving probes, valid inside this context. terminal.update(pid=child.pid, input_fd=master) if launch is not None and socket_name is None: - address = root / f'pardes-9p-{child.pid}.sock' + # Named by the session's pid: a terminal's session is forked + # off the process launched, which stays as its stub. + address = None if slave is not None: os.close(slave) slave = None @@ -68,7 +73,11 @@ def session(binary, root, name, *options, socket_name=None, file=None, tty=False reader = threading.Thread(target=drain, daemon=True) reader.start() deadline = time.monotonic() + 10 - while not address.exists(): + while address is None or not address.exists(): + if address is None: + address = next(iter(set(root.glob('pardes-9p-*.sock')) - existing), None) + if address is not None: + continue if child.poll() is not None: raise AssertionError((args, child.returncode, child.stderr.read().decode())) if time.monotonic() > deadline: @@ -512,8 +521,10 @@ def run_file(binary): assert b'\x1b' not in client.read(f'/pane/{pager[0]}/body') client.remove(f'/pane/{pager[0]}') # systemd's tools page through their own variable: set too, its - # secure mode off, the path unquoted where it needs none. - systemd_pager = run(client, term, b'printenv SYSTEMD_PAGER SYSTEMD_PAGERSECURE\n') + # secure mode off, the path unquoted where it needs none. Read + # by the shell: coreutils 9.12's printenv quotes a value with a + # blank on a terminal. + systemd_pager = run(client, term, b'printf "%s\\n" "$SYSTEMD_PAGER" "$SYSTEMD_PAGERSECURE"\n') assert systemd_pager.startswith(b'exit 0\n') and systemd_pager.rstrip().endswith(b' -\n0'), systemd_pager if shutil.which('journalctl'): assert run(client, term, b'journalctl -n 3 --no-hostname\n').startswith(b'exit ') @@ -544,8 +555,9 @@ def run_file(binary): time.sleep(.05) locked.chmod(0o600) try: - client.write(f'/pane/{term}/pty/ctl', b'exec\n') - raise AssertionError('pty/ctl exec of a shell that cannot start was taken') + if os.geteuid() != 0: # root enters any directory + client.write(f'/pane/{term}/pty/ctl', b'exec\n') + raise AssertionError('pty/ctl exec of a shell that cannot start was taken') except OSError as refused: assert 'shell' in str(refused), refused finally: @@ -984,19 +996,24 @@ def python_repl_runs_one_line(binary): def signal_cleans_up(binary): - """A detached session told to stop by a signal unlinks its sockets.""" + """A detached session told to stop by a signal unlinks its sockets. A + hangup is a terminal's end, not a session's: the daemon keeps it.""" for sig in [signal.SIGTERM, signal.SIGINT, signal.SIGHUP]: with tempfile.TemporaryDirectory(prefix='pardes-sig-') as directory: root = Path(directory) seen = {} with session(binary, root, 'sig', terminal=seen) as (client, address): - detached = root / 'pardes-detached-sig.sock' - assert address.exists() and detached.exists() + assert address.exists() + if sig == signal.SIGHUP: + os.kill(seen['pid'], sig) + time.sleep(.3) + assert address.exists() and client.read('/index'), 'a hangup ended a kept session' + sig = signal.SIGTERM os.kill(seen['pid'], sig) deadline = time.monotonic() + 5 - while (address.exists() or detached.exists()) and time.monotonic() < deadline: + while address.exists() and time.monotonic() < deadline: time.sleep(.01) - assert not address.exists() and not detached.exists(), (sig, list(root.iterdir())) + assert not address.exists(), (sig, list(root.iterdir())) def odd_paths(binary): @@ -1422,10 +1439,9 @@ def test(binary, quic=False): # The new log names its panes, then where they came from. log = restored.read('/log') assert b'\nrestore ' + str(saved).encode() + b'\n' in log, log - # then which serial each pane had: restored <old> <new> - assert (b'\n'.join(b'restored %d %d' % (old, new) for old, new in zip(sorted(old_ids), sorted(int(r.split()[0]) for r in restored.read('/index').splitlines()))) + b'\nrestoredcol ') in log, log + # Its panes are the dump's, their serials too. restored_ids = [int(row.split()[0]) for row in restored.read('/index').splitlines()] - assert len(restored_ids) == 2 and old_ids.isdisjoint(restored_ids), restored_ids + assert len(restored_ids) == 2 and set(restored_ids) == old_ids, restored_ids file_id, control_id = restored_ids contents = restored.read(f'/pane/{file_id}/body') assert contents == b'dumped state\n', (contents, saved.read_text(), restored.read('/index')) @@ -1651,15 +1667,17 @@ def test(binary, quic=False): client.remove(f'/pane/{dashed}') # A name under a directory that may not be searched is refused, # permission denied, exit 1, no pane: its Save could never be. - (root / 'noperm').mkdir() - (root / 'noperm').chmod(0) - before = serials() - try: - denied = subprocess.run([binary, 'noperm/a/b/c'], cwd=root, env=env, capture_output=True, timeout=10) - finally: - (root / 'noperm').chmod(0o755) - assert denied.returncode == 1 and b'permission denied' in denied.stderr, denied - assert serials() == before + # (Root searches any directory.) + if os.geteuid() != 0: + (root / 'noperm').mkdir() + (root / 'noperm').chmod(0) + before = serials() + try: + denied = subprocess.run([binary, 'noperm/a/b/c'], cwd=root, env=env, capture_output=True, timeout=10) + finally: + (root / 'noperm').chmod(0o755) + assert denied.returncode == 1 and b'permission denied' in denied.stderr, denied + assert serials() == before # --wait waits on the pane its look answered, on the look's own # open: a name /index shows escaped (a backslash is `\\` there) # is still the pane waited on, not one already gone. @@ -1747,34 +1765,43 @@ def test(binary, quic=False): assert f'\nnew {made_now} {root}/+Pager\n' in '\n' + log_now, log_now[-400:] assert f'\nrename {made_now} ' not in '\n' + log_now # Its colours are the pane's, drawn and never read: the body is - # the plain text, and the screen shows `red` in the theme's red - # and `rgb` in the colour given, the rest in the pane's ink. + # the plain text, and the screen shows `red` in the theme's red, + # the rest in the pane's ink. Mapped through the theme, as a + # terminal's are; Filter shows them as sent, `rgb` in the colour + # given. + # Each run is its own +Pager; one there already keeps its page. fed = subprocess.run([binary, '-'], cwd=root, env=env, input=b'\x1b]0;title\x07\x1b[31mred\x1b[m \x1b[38;2;1;2;3mrgb\x1b[m plain\n', stdout=subprocess.PIPE, stderr=subprocess.PIPE, timeout=10) assert fed.returncode == 0, fed.stderr - assert client.read(f'/pane/{made_now}/body') == b'red rgb plain\n' - screen = client.screen() - for row in range(screen['rows']): - cells = screen['cells'][row * screen['cols']:(row + 1) * screen['cols']] - text = ''.join(cell[0] for cell in cells) - at = text.find('red rgb plain') - if at >= 0: - break - else: + coloured, = serials() - before - {made_now} + assert client.read(f'/pane/{coloured}/name') == f'{root}/+Pager\n'.encode() + assert client.read(f'/pane/{coloured}/body') == b'red rgb plain\n' + assert client.read(f'/pane/{made_now}/body') == b'bold text\n' + def inks(): + screen = client.screen() + for row in range(screen['rows']): + cells = screen['cells'][row * screen['cols']:(row + 1) * screen['cols']] + at = ''.join(cell[0] for cell in cells).find('red rgb plain') + if at >= 0: + return [screen['styles'][cells[at + i][1]]['fg'] for i in (0, 4, 8)] raise AssertionError(('the paged text is not on the screen', screen)) - ink = [screen['styles'][cells[at + i][1]]['fg'] for i in (0, 4, 8)] + ink = inks() + assert ink[0] != ink[2] and ink[1] != {'rgb': [1, 2, 3]}, ink + client.write(f'/pane/{coloured}/exec', b'Filter\n') + ink = inks() assert ink[1] == {'rgb': [1, 2, 3]}, ink assert ink[0] != ink[2] and ink[0] != ink[1], ink - # The directory's one +Pager: the next paged text refills it. + # A progress line keeps its last state. fed = subprocess.run([binary, '-'], cwd=root, env=env, input=b'again\r\nprogress 10%\rprogress 100%\n', stdout=subprocess.PIPE, stderr=subprocess.PIPE, timeout=10) assert fed.returncode == 0, fed.stderr - assert serials() - before == {made_now}, (serials(), before) - assert client.read(f'/pane/{made_now}/body') == b'again\nprogress 100%\n' - client.remove(f'/pane/{made_now}') + again, = serials() - before - {made_now, coloured} + assert client.read(f'/pane/{again}/body') == b'again\nprogress 100%\n' + for serial in (made_now, coloured, again): + client.remove(f'/pane/{serial}') # More than one 8 MiB write's worth arrives whole, byte for byte, - # in the one +Pager (each write once replaced the last). + # in its +Pager (each write once replaced the last). big = b''.join(b'line %08d of the paged text\n' % i for i in range(400000)) # ~11 MiB assert len(big) > 8 * 1024 * 1024 fed = subprocess.run([binary, '-'], cwd=root, env=env, input=big, @@ -1789,7 +1816,8 @@ def test(binary, quic=False): stdout=subprocess.PIPE, stderr=subprocess.PIPE, timeout=600) huge.wait() assert fed.returncode == 0, fed.stderr - assert serials() - before == {big_pane} + client.remove(f'/pane/{big_pane}') + big_pane, = serials() - before got = client.read(f'/pane/{big_pane}/body') note = b'\n[pardes: the rest of stdin is cut: a pane holds at most a file\'s limit]\n' assert got.endswith(note) and len(got) == cap + len(note), (len(got), got[-120:]) @@ -1812,16 +1840,17 @@ def test(binary, quic=False): stdout=subprocess.PIPE, stderr=subprocess.PIPE, timeout=10) assert fed.returncode == 0 and serials() == before, (fed, serials()) # A new name in a directory there but not writable: refused now, - # not a pane that only fails at Save. + # not a pane that only fails at Save. (Root writes anywhere.) shut = root / 'shut-dir' shut.mkdir() shut.chmod(0o500) try: - refused = subprocess.run([binary, 'shut-dir/new.txt'], cwd=root, env=env, - stdout=subprocess.PIPE, stderr=subprocess.PIPE, timeout=10) - assert refused.returncode == 1, refused - assert b'may not be read or written' in refused.stderr, refused.stderr - assert serials() == before, (serials(), before) + if os.geteuid() != 0: + refused = subprocess.run([binary, 'shut-dir/new.txt'], cwd=root, env=env, + stdout=subprocess.PIPE, stderr=subprocess.PIPE, timeout=10) + assert refused.returncode == 1, refused + assert b'may not be read or written' in refused.stderr, refused.stderr + assert serials() == before, (serials(), before) finally: shut.chmod(0o755) for word, said in (('@p999:1', b'pardes: @p999:1: this session has no such pane'), @@ -1885,6 +1914,7 @@ def test(binary, quic=False): cooked = termios.tcgetattr(slave) env = {k: v for k, v in os.environ.items() if not k.startswith('PARDES_')} env.update(HOME=str(root), XDG_RUNTIME_DIR=str(root), TERM='xterm-256color', PARDES_NOTIME='1') + existing = set(root.glob('pardes-9p-*.sock')) killed = subprocess.Popen([binary, '--tty', 'x.txt'], cwd=root, env=env, stdin=slave, stdout=slave, stderr=subprocess.DEVNULL, start_new_session=True, @@ -1897,12 +1927,21 @@ def test(binary, quic=False): if select.select([master], [], [], .1)[0]: seen += os.read(master, 65536) time.sleep(.3) - own_socket = root / f'pardes-9p-{killed.pid}.sock' - assert own_socket.exists() + # The session's own, named by its pid: forked off the + # launched process, which stays as its stub. + own_socket, = set(root.glob('pardes-9p-*.sock')) - existing assert not dead_socket.exists() assert live_socket.exists() killed.send_signal(sig) assert killed.wait(timeout=5) == -sig + if sig == signal.SIGHUP: + # A hangup is the terminal's end: the session carries on + # detached (the daemon keeps it) until told to end. + assert own_socket.exists(), own_socket + os.kill(int(own_socket.name[len('pardes-9p-'):-len('.sock')]), signal.SIGTERM) + deadline = time.monotonic() + 5 + while own_socket.exists() and time.monotonic() < deadline: + time.sleep(.01) assert not own_socket.exists(), own_socket while select.select([master], [], [], .2)[0]: try: diff --git a/test/fs_namespace.zig b/test/fs_namespace.zig index 30c656ca..c68a438b 100644 --- a/test/fs_namespace.zig +++ b/test/fs_namespace.zig @@ -100,13 +100,14 @@ test "transactional restore preserves host capabilities and advances pane identi try std.testing.expectEqual(@as(u32, 73), restored.pipe.seq); try std.testing.expectEqual(p.cell_pixels, restored.cell_pixels); try std.testing.expect(restored.native_images); - try std.testing.expect(restored.panes[0].?.serial > p.next_serial); - try std.testing.expect(restored.paneBySerial(pane.serial) == null); + // The dump's pane, its serial too; a new one is past the old session's. + try std.testing.expectEqual(pane.serial, restored.panes[0].?.serial); + try std.testing.expect(restored.next_serial >= p.next_serial); try std.testing.expectEqual(p.screen_w, restored.screen_w); try std.testing.expectEqual(p.screen_h, restored.screen_h); try std.testing.expect(p.panes[0] == pane); try std.testing.expectEqualStrings("before\n", pane.file.?.content); - try std.testing.expectError(error.BadDumpMagic, pardes.dump.restore( + try std.testing.expectError(error.BadDump, pardes.dump.restore( p, ".{ .magic = \"wrong\", .screen = .{.cols = 80, .rows = 24} }", "test", @@ -201,23 +202,24 @@ test "explicit OS file opens and restores keep their names and request watches" const pane = try pardes.panes.File.open(p, opened, path, 0); try std.testing.expectEqualStrings(path, pane.file.?.path); try std.testing.expectEqualStrings("const disk = 1;\n", pane.file.?.content); - const restored = p.freeSlot().?; - const copy = try pardes.panes.File.restore(p, restored, .{ - .kind = .file, - .tag = path, - .body = "", - // Unsaved text: kept (a clean pane's is read from disk). - .file = .{ .path = path, .content = "const unsaved = 2;\n", .dirty = true }, - }); - try std.testing.expectEqualStrings(path, copy.file.?.path); - try std.testing.expectEqualStrings("const unsaved = 2;\n", copy.file.?.content); var watched_open = false; - var watched_restore = false; while (p.nextEffect()) |effect| if (effect == .watch and effect.watch.on) { if (effect.watch.pane == opened) watched_open = true; - if (effect.watch.pane == restored) watched_restore = true; }; - try std.testing.expect(watched_open and watched_restore); + try std.testing.expect(watched_open); + // Unsaved, dumped and loaded: its text kept, its file watched again. + _ = pardes.ctlfs.pane.spliceBody(p, pane, 0, 0, "// unsaved\n").?; + try pardes.dump.dumpState(p); + const restored = try pardes.dump.initFromDump(gpa, .{ .tty_only = true }, p.dump_out.?); + defer restored.deinit(); + const copy = restored.panes[opened].?; + try std.testing.expectEqualStrings(path, copy.file.?.path); + try std.testing.expectEqualStrings("// unsaved\nconst disk = 1;\n", copy.file.?.content); + var watched_restore = false; + while (restored.nextEffect()) |effect| if (effect == .watch and effect.watch.on) { + if (effect.watch.pane == opened) watched_restore = true; + }; + try std.testing.expect(watched_restore); } test "virtual file opens do not request OS watches" { @@ -308,9 +310,9 @@ test "images decode through explicit filesystem mounts, and a dump reads one on const state = pane.image.?; try std.testing.expectEqual(@as(usize, 1), state.iw); try std.testing.expectEqual(@as(usize, 1), state.ih); - // On disk, the picture is no part of the dump: Restore reads it there. - const saved = try pardes.panes.Image.dumpPane(p, arena.allocator(), pane, "", "", 0, path, state.raw); - try std.testing.expectEqualStrings("", saved.image.?.bytes_b64); - const restored = try pardes.panes.Image.restore(p, p.freeSlot().?, saved); - try std.testing.expectEqualStrings(bytes, restored.image.?.raw); + // The picture's bytes are in the dump: it comes back as it was shown. + try pardes.dump.dumpState(p); + const restored = try pardes.dump.initFromDump(gpa, .{ .tty_only = true }, p.dump_out.?); + defer restored.deinit(); + try std.testing.expectEqualStrings(bytes, restored.panes[0].?.image.?.raw); } diff --git a/test/kitty.zig b/test/kitty.zig index de4dd605..1e1e8331 100644 --- a/test/kitty.zig +++ b/test/kitty.zig @@ -299,6 +299,8 @@ test "kitty: file, temporary-file and shared-memory media load, and the terminal const fd = shm.shm_open(name, @bitCast(std.c.O{ .ACCMODE = .RDWR, .CREAT = true, .TRUNC = true }), 0o600); if (fd < 0) return error.ShmOpenFailed; defer _ = shm.shm_unlink(name); + // Sized first: FreeBSD's write does not grow a shared memory object. + if (ftruncate(fd, rgb_raw.len) != 0) return error.WriteFailed; if (std.c.write(fd, &rgb_raw, rgb_raw.len) != rgb_raw.len) return error.WriteFailed; _ = std.c.close(fd); r.feed(0, try std.fmt.allocPrint(arena, "\x1b_Ga=t,t=s,i=43,f=24,s=4,v=2,S={d};{s}\x1b\\", .{ rgb_raw.len, try b64(arena, name) })); @@ -847,6 +849,8 @@ test "kitty: a FIFO named by t=f, t=t or t=s never blocks pardes" { .{ .medium = 's', .fifo = shm_path, .name = shm_path["/dev/shm".len..] }, }; for (cases) |c| { + // Shared memory is a directory only on Linux (/dev/shm). + if (c.medium == 's' and comptime @import("builtin").os.tag != .linux) continue; _ = std.c.unlink(c.fifo); if (mkfifo(c.fifo, 0o600) != 0) return error.MkfifoFailed; defer _ = std.c.unlink(c.fifo); @@ -1237,11 +1241,11 @@ test "kitty: a dumped and restored terminal keeps its images" { defer r.deinit(); r.feed(0, "\x1b[1;1Hpicture" ++ apc("a=T,i=25,f=24,s=4,v=2,c=2,r=1,q=2", rgb_b64)); try pardes.dump.dumpState(r.p); - const restored = try pardes.dump.restore(r.p, r.p.dump_out.?, "/tmp/pardes-kitty.dump.zon"); + const restored = try pardes.dump.restore(r.p, r.p.dump_out.?, "/tmp/pardes-kitty.dump"); defer restored.deinit(); const pane = restored.panes[0].?; - const kept = pane.terminal != null and pane.terminal.?.vt.screens.active.kitty_images.placements.count() > 0; - try knownGap("a terminal's kitty images do not survive a dump (Dump/Load, Handover, a closed pane reopened from the Store): dumpPane writes the screen as VT text (ScreenFormatter), which has no images; ghostty's snapshot format carries them but is unstable (v1)", kept); + // ghostty's snapshot of the emulator (src/terminal.zig) carries them. + try std.testing.expect(pane.terminal.?.vt.screens.active.kitty_images.placements.count() > 0); } // ---- Real clients' traffic ---- diff --git a/test/kitty_client.zig b/test/kitty_client.zig index 753727df..03ad629e 100644 --- a/test/kitty_client.zig +++ b/test/kitty_client.zig @@ -17,6 +17,23 @@ pub const side = 64; pub const colour = [3]u8{ 13, 77, 231 }; const sync = "\x1b_Gi=999,s=1,v=1,a=q,t=d,f=24;AAAA\x1b\\"; +// The kernel's ioctls on Linux: glibc's tcgetattr writes its 60-byte +// termios into Zig's 44-byte kernel one, and std.posix's calls glibc's in a +// program linking libc (src/main.zig exports kernel ones; this has none). +const linux = @import("builtin").os.tag == .linux; + +fn getTermios() !std.posix.termios { + if (!linux) return std.posix.tcgetattr(0) catch error.NotATerminal; + var t: std.os.linux.termios = undefined; + if (std.os.linux.errno(std.os.linux.tcgetattr(0, &t)) != .SUCCESS) return error.NotATerminal; + return t; +} + +fn setTermios(t: std.posix.termios) void { + if (!linux) return std.posix.tcsetattr(0, .NOW, t) catch {}; + _ = std.os.linux.tcsetattr(0, .NOW, &t); +} + pub fn main(init: std.process.Init) !void { var arena_state: std.heap.ArenaAllocator = .init(std.heap.page_allocator); defer arena_state.deinit(); @@ -26,15 +43,14 @@ pub fn main(init: std.process.Init) !void { const case = args[1]; const out = try arena.dupeSentinel(u8, args[2], 0); - var saved: c.termios = undefined; - if (c.tcgetattr(0, &saved) != 0) return error.NotATerminal; + const saved = try getTermios(); var raw = saved; raw.lflag.ICANON = false; raw.lflag.ECHO = false; - raw.cc[@backingInt(c.V.MIN)] = 0; - raw.cc[@backingInt(c.V.TIME)] = 1; - _ = c.tcsetattr(0, .NOW, &raw); - defer _ = c.tcsetattr(0, .NOW, &saved); + raw.cc[@backingInt(std.posix.V.MIN)] = 0; + raw.cc[@backingInt(std.posix.V.TIME)] = 1; + setTermios(raw); + defer setTermios(saved); var cmds: std.ArrayList(u8) = .empty; try play(arena, case, &cmds); diff --git a/test/monkey.zig b/test/monkey.zig index dcea0b43..2e19cb21 100644 --- a/test/monkey.zig +++ b/test/monkey.zig @@ -1,6 +1,6 @@ // pardes-monkey: random snapshot scripts against the tty, hunting panics. // -// zig build monkey -Dplatform=tty -- <seeds> [steps] [--from=N] [--out=DIR] [--keep] +// zig build monkey -Dplatform=tty -- <seeds> [steps] [--from=N] [--out=DIR] [--keep] [--views=N] // // Each seed makes one script (a fixed seed makes the same script, so a hit // replays): keys and leader bursts, typing, mouse presses, drags and wheel, @@ -9,7 +9,9 @@ // /tmp/pardes-monkey), and a panic is a record in the app's crash file // (src/crash.zig, in the script's fake home: found even when the crash cut // the script short) or a `.zig:N:N: 0x` stack frame in the report or the -// grid it captured (a trace renders INTO the screen). A hit +// grid it captured (a trace renders INTO the screen). --views=N gives the +// session N terminals, each a view of it (views.zig), and types into and +// sizes each in turn, a `view` switch among the steps. A hit // keeps DIR/seed-N.snap, its captures and its report; the rest are deleted. // A panic's trace can scroll off a small grid: replay the kept script, or // run the app in a larger pty. Every hit gets a fix and a pinned regression @@ -26,6 +28,9 @@ const Script = struct { gpa: std.mem.Allocator, rows: u16 = 24, cols: u16 = 80, + /// Each terminal's size, and the one typed into (`--views`). + sizes: [4][2]u16 = @splat(.{ 24, 80 }), + view: usize = 0, fn line(s: *Script, comptime fmt: []const u8, args: anytype) !void { try s.out.print(s.gpa, fmt ++ "\n", args); @@ -38,18 +43,24 @@ const commands = [_][]const u8{ "Newcol", "Find", "Grep", "Joincol", "Help", "Du const leader_keys = "abcdefghijklmnopqrstuvwxyz0123456789$^"; const typed = "abcdefghijklmnopqrstuvwxyz0123456789 -_=+,:;()"; -fn generate(gpa: std.mem.Allocator, seed: u64, steps: usize) ![]u8 { +fn generate(gpa: std.mem.Allocator, seed: u64, steps: usize, views: usize) ![]u8 { var prng = std.Random.DefaultPrng.init(seed); const random = prng.random(); var s: Script = .{ .gpa = gpa }; - try s.line("# pardes-monkey seed {d}, {d} steps", .{ seed, steps }); + try s.line("# pardes-monkey seed {d}, {d} steps, {d} views", .{ seed, steps, views }); try s.line("file bad.txt hello \\xff\\xfe world\\nlatin-1 caf\\xe9 tail\\ntruncated \\xe6\\x97 here\\nend\\n", .{}); try s.line("file wide.txt {s}tail\\nsecond line\\n", .{std.mem.asBytes(&@as([8][37]u8, @splat("\\xe6\\x97\\xa5\\xe6\\x9c\\xac\\xe8\\xaa\\x9e ".*)))}); try s.line("lines long.txt 400 some words after the number, long enough to wrap in a narrow pane", .{}); - try s.line("start 24 80 bad.txt", .{}); + if (views > 1) try s.line("start 24 80 views={d} bad.txt", .{views}) else try s.line("start 24 80 bad.txt", .{}); try s.line("wait 8000 Newcol Joincol", .{}); try s.line("stable 300 5000", .{}); for (0..steps) |step| { + if (views > 1 and random.uintLessThan(u8, 4) == 0) { + s.sizes[s.view] = .{ s.rows, s.cols }; + s.view = random.uintLessThan(usize, views); + s.rows, s.cols = s.sizes[s.view]; + try s.line("view {d}", .{s.view + 1}); + } const wide = s.cols >= 70 and s.rows >= 6; switch (random.uintLessThan(u8, 12)) { // A workspace word, run with a middle click: new columns make @@ -150,9 +161,9 @@ test "a panic trace is found, and ordinary colons are not" { } test "a seed makes the same script every time" { - const a = try generate(std.testing.allocator, 42, 50); + const a = try generate(std.testing.allocator, 42, 50, 3); defer std.testing.allocator.free(a); - const b = try generate(std.testing.allocator, 42, 50); + const b = try generate(std.testing.allocator, 42, 50, 3); defer std.testing.allocator.free(b); try std.testing.expectEqualStrings(a, b); } @@ -166,7 +177,7 @@ pub fn main(init: std.process.Init) !void { const io = init.io; const args = try init.minimal.args.toSlice(init.arena.allocator()); if (args.len < 4) { - std.debug.print("usage: pardes-monkey <pardes-snap> <pardes> <seeds> [steps] [--from=N] [--out=DIR]\n", .{}); + std.debug.print("usage: pardes-monkey <pardes-snap> <pardes> <seeds> [steps] [--from=N] [--out=DIR] [--keep] [--tape=DIR] [--views=N] [--dump]\n", .{}); std.process.exit(2); } const snap = args[1]; @@ -177,12 +188,25 @@ pub fn main(init: std.process.Init) !void { var out: []const u8 = "/tmp/pardes-monkey"; // Keep every script and capture, not only a hit's: to see what it did. var keep = false; + // --tape=DIR: each seed's sessions record their tapes there and the + // seed is a hit if one does not replay (pardes-snap --tape). + var tape: ?[]const u8 = null; + var views: usize = 1; + // --dump (with --tape): each replay loads the session's dump at its end + // too (pardes-snap --dump). + var dump = false; var positional: usize = 0; for (args[3..]) |a| { - if (std.mem.startsWith(u8, a, "--from=")) { + if (std.mem.startsWith(u8, a, "--views=")) { + views = std.math.clamp(try std.fmt.parseInt(usize, a["--views=".len..], 10), 1, 4); + } else if (std.mem.startsWith(u8, a, "--from=")) { from = try std.fmt.parseInt(u64, a["--from=".len..], 10); } else if (std.mem.eql(u8, a, "--keep")) { keep = true; + } else if (std.mem.startsWith(u8, a, "--tape=")) { + tape = a; + } else if (std.mem.eql(u8, a, "--dump")) { + dump = true; } else if (std.mem.startsWith(u8, a, "--out=")) { out = a["--out=".len..]; } else { @@ -198,13 +222,13 @@ pub fn main(init: std.process.Init) !void { var hits: usize = 0; var seed = from; while (seed < from + seeds) : (seed += 1) { - const script = try generate(gpa, seed, steps); + const script = try generate(gpa, seed, steps, views); defer gpa.free(script); var path_buf: [4096]u8 = undefined; const path = try std.fmt.bufPrint(&path_buf, "{s}/seed-{d}.snap", .{ out, seed }); try std.Io.Dir.cwd().writeFile(io, .{ .sub_path = path, .data = script }); const run = std.process.run(gpa, io, .{ - .argv = &.{ snap, app, "--update", "--no-retry", "--jobs=1", path }, + .argv = if (tape) |t| (if (dump) &.{ snap, app, "--update", "--no-retry", "--jobs=1", t, "--dump", path } else &.{ snap, app, "--update", "--no-retry", "--jobs=1", t, path }) else &.{ snap, app, "--update", "--no-retry", "--jobs=1", path }, .stdout_limit = .limited(64 << 20), .stderr_limit = .limited(64 << 20), }) catch |err| { @@ -223,14 +247,15 @@ pub fn main(init: std.process.Init) !void { const crash_path = try std.fmt.bufPrint(&crash_buf, "/tmp/pardes-snap/seed-{d}/home/.config/pardes/crashes", .{seed}); const crashes = readAll(gpa, io, crash_path); defer gpa.free(crashes); - if (crashes.len != 0 or hasTrace(run.stdout) or hasTrace(run.stderr) or hasTrace(golden)) { + const diverged = tape != null and (std.mem.indexOf(u8, run.stdout, "DIVERGED") != null or std.mem.indexOf(u8, run.stderr, "DIVERGED") != null); + if (crashes.len != 0 or diverged or hasTrace(run.stdout) or hasTrace(run.stderr) or hasTrace(golden)) { hits += 1; var log_buf: [4096]u8 = undefined; const log_path = try std.fmt.bufPrint(&log_buf, "{s}/seed-{d}.log", .{ out, seed }); const both = try std.mem.concat(gpa, u8, &.{ crashes, run.stdout, run.stderr }); defer gpa.free(both); try std.Io.Dir.cwd().writeFile(io, .{ .sub_path = log_path, .data = both }); - std.debug.print("PANIC seed {d}: {s}\n", .{ seed, path }); + std.debug.print("{s} seed {d}: {s}\n", .{ if (diverged) "DIVERGED" else "PANIC", seed, path }); } else if (!keep) { std.Io.Dir.cwd().deleteFile(io, path) catch {}; std.Io.Dir.cwd().deleteFile(io, golden_path) catch {}; diff --git a/test/output.zig b/test/output.zig index 530db77c..2b8bf1f5 100644 --- a/test/output.zig +++ b/test/output.zig @@ -495,23 +495,6 @@ test "Mini syntax colors survive toggles themes and rendering without source acc } } -test "Mini restore rejects inconsistent metadata before publishing a pane" { - const p = try Pardes.init(std.testing.allocator, .{ .tty_only = true }); - defer p.deinit(); - const id = p.freeSlot().?; - const serial = p.next_serial; - for ([_]pardes.dump.File{ - .{ .content = "x", .mini_source = "/source.zig", .mini_colors_b64 = "AA==" }, - .{ .content = "x", .origin = "Mini", .mini_source = "/source.zig", .mini_colors_b64 = "" }, - .{ .content = "x", .origin = "Mini", .mini_source = "/source.zig", .mini_colors_b64 = "BQ==" }, - .{ .content = "x", .origin = "Mini", .mini_colors_b64 = "AA==" }, - }) |saved| { - try std.testing.expectError(error.InvalidMini, panes.File.restore(p, id, .{ .kind = .file, .tag = "", .body = "", .file = saved })); - try std.testing.expect(p.panes[id] == null); - try std.testing.expectEqual(serial, p.next_serial); - } -} - test "Mini failures leave its existing snapshot and focus unchanged" { const p = try Pardes.init(std.testing.allocator, .{ .tty_only = true }); defer p.deinit(); @@ -701,7 +684,8 @@ test "every enabled setting builtin mutates the State Config reports" { var chosen_font: ?[]const u8 = null; var buf: [512]u8 = undefined; for (config.Runtime.settings) |setting| { - if (!setting.enabled(builtins.capabilities)) continue; + // The view's own (a terminal's takes no window's words). + if (!setting.enabled(builtins.viewCapabilities(p))) continue; const command: []const u8 = switch (setting.action) { .theme => try std.fmt.bufPrint(&buf, "{s} acme", .{setting.word}), .shell => try std.fmt.bufPrint(&buf, "{s} fish", .{setting.word}), @@ -733,7 +717,7 @@ test "every enabled setting builtin mutates the State Config reports" { "Theme acme\n", "Shell fish\n", }) |line| try std.testing.expect(std.mem.indexOf(u8, report, line) != null); - const transition = if (!builtins.capabilities.panel_transitions) + const transition = if (!builtins.viewCapabilities(p).panel_transitions) "PanelSlide unsupported\n" else if (p.settings.panel_transition == .off) "PanelSlide off\n" @@ -741,12 +725,12 @@ test "every enabled setting builtin mutates the State Config reports" { try std.fmt.bufPrint(&buf, "{s} on\n", .{config.Runtime.findAction(.{ .transition = p.settings.panel_transition }).?.word}); try std.testing.expect(std.mem.indexOf(u8, report, transition) != null); // A bare word turns a bundled pass on at its default level. - const scene_status = if (builtins.capabilities.scene_shaders) "2" else "unsupported"; + const scene_status = if (builtins.viewCapabilities(p).scene_shaders) "2" else "unsupported"; for ([_][]const u8{"Crt"}) |name| { const line = try std.fmt.bufPrint(&buf, "{s} {s}\n", .{ name, scene_status }); try std.testing.expect(std.mem.indexOf(u8, report, line) != null); } - const tagline = if (!builtins.capabilities.tagline_font_size) + const tagline = if (!builtins.viewCapabilities(p).tagline_font_size) "TaglineSize unsupported\n" else try std.fmt.bufPrint(&buf, "TaglineSize {d}\n", .{p.settings.font.tagline_percent}); diff --git a/test/panes.zig b/test/panes.zig index e2cd85dc..fc3f7804 100644 --- a/test/panes.zig +++ b/test/panes.zig @@ -587,6 +587,7 @@ const BodyHitTests = struct { pane.scrollBy(10); var frame = std.heap.ArenaAllocator.init(p.gpa); defer frame.deinit(); + p.sync(); _ = try p.render(frame.allocator()); const layer = &p.surface.body_layers[0]; try std.testing.expect(layer.context_rows >= 4); @@ -635,6 +636,7 @@ const LineNumberLookTests = struct { pane.scrollBy(30); var frame = std.heap.ArenaAllocator.init(p.gpa); defer frame.deinit(); + p.sync(); _ = try p.render(frame.allocator()); const layer = &p.surface.body_layers[0]; try std.testing.expect(layer.context_rows >= 2); @@ -693,6 +695,7 @@ const TreeContextTests = struct { pane.scrollBy(10); var frame = std.heap.ArenaAllocator.init(p.gpa); defer frame.deinit(); + p.sync(); _ = try p.render(frame.allocator()); const layer = &p.surface.body_layers[0]; try std.testing.expectEqual(@as(u16, 4), layer.context_rows); @@ -747,9 +750,11 @@ const TreeContextTests = struct { var frame = std.heap.ArenaAllocator.init(p.gpa); defer frame.deinit(); pane.scrollBy(7); + p.sync(); _ = try p.render(frame.allocator()); try std.testing.expectEqual(@as(u16, 0), pane.context_rows); try std.testing.expect(p.executeBuiltinLine(0, "TreeContext")); + p.sync(); _ = try p.render(frame.allocator()); try std.testing.expectEqual(@as(u16, 4), pane.context_rows); for (0..4) |row| try std.testing.expectEqual(@as(i32, @intCast(row)), pane.wrapAt(@intCast(row)).line); @@ -803,6 +808,7 @@ const TreeContextTests = struct { pane.hscroll = 4; var frame = std.heap.ArenaAllocator.init(p.gpa); defer frame.deinit(); + p.sync(); const surface = try p.render(frame.allocator()); try std.testing.expectEqual(@as(u16, 3), pane.context_rows); const rect = p.rects[0]; @@ -822,6 +828,7 @@ const TreeContextTests = struct { // builtin above put up, so the body moved with it. try std.testing.expectEqual(p.bodyTop(pane, p.rects[0]), cursor.y); panes.File.setContent(p, &pane.file.?, try p.gpa.dupe(u8, std.mem.asBytes(&@as([18][28]u8, @splat("// no enclosing declaration\n".*))))); + p.sync(); _ = try p.render(frame.allocator()); try std.testing.expectEqual(@as(u16, 0), pane.context_rows); try std.testing.expectEqual(pane.scroll(), pane.wrapAt(0).line); @@ -840,6 +847,7 @@ const TreeContextTests = struct { pane.scrollBy(2); var frame = std.heap.ArenaAllocator.init(p.gpa); defer frame.deinit(); + p.sync(); const surface = try p.render(frame.allocator()); try std.testing.expectEqual(@as(u16, 1), pane.context_rows); try std.testing.expectEqual(@as(i32, 2), pane.wrapAt(1).line); @@ -2289,7 +2297,7 @@ test "a prompt opens with one allocation" { } test "file history construction failures preserve existing panes" { - const Case = enum { open, restore, output, tutor, replace }; + const Case = enum { open, output, tutor, replace }; for (std.enums.values(Case)) |case| { var succeeded = false; for (0..32) |failure| { @@ -2306,12 +2314,6 @@ test "file history construction failures preserve existing panes" { failing.fail_index = failing.alloc_index + failure; const result = switch (case) { .open => panes.File.open(p, p.freeSlot().?, path, 0), - .restore => panes.File.restore(p, 1, .{ - .kind = .file, - .tag = "", - .body = "", - .file = .{ .path = "/restored.zig", .content = "restored\n" }, - }), .output => panes.Output.open(p, p.freeSlot().?, "/", .{ .cmd = .Help }, "", incoming.?), .tutor => p.openTutorView(1), .replace => p.setTestFile("replacement\n"), @@ -2397,39 +2399,6 @@ test "file history allocation precedes reading the served tree" { } } -test "file history survives PDF fallback restoration and allocation failures" { - const saved = - \\.{ - \\ .screen = .{ .cols = 40, .rows = 12 }, - \\ .columns = .{.{ .panes = .{0} }}, - \\ .panes = .{.{ - \\ .kind = .image, .tag = "", .body = "", - \\ .image = .{ .path = "/virtual/missing-history.pdf", .bytes_b64 = "cmF3" }, - \\ }}, - \\} - ; - var succeeded = false; - for (0..256) |failure| { - var failing = std.testing.FailingAllocator.init(std.testing.allocator, .{ .fail_index = failure }); - if (pardes.dump.initFromDump(failing.allocator(), .{ .tty_only = true }, saved)) |p| { - defer p.deinit(); - const pane = p.panes[0].?; - try std.testing.expect(pane.file != null); - try std.testing.expectEqualStrings("raw", pane.file.?.content); - try std.testing.expectEqual(@as(usize, 0), pane.file.?.history.undo_len); - try std.testing.expectEqual(@as(usize, 0), pane.file.?.history.redo_len); - if (!failing.has_induced_failure) { - succeeded = true; - break; - } - } else |err| { - try std.testing.expect(failing.has_induced_failure); - try std.testing.expect(err == error.OutOfMemory or err == error.WriteFailed); - } - } - try std.testing.expect(succeeded); -} - test "Save rearms renamed file watches only after a successful write" { const Host = struct { const Result = enum { success, failure, edited, replaced, closed }; @@ -2661,6 +2630,9 @@ const FileTests = struct { try std.testing.expectEqual(text.len, byteAtDisplay(text, 5)); try std.testing.expectEqual(@as(usize, 3), rawDisplayCol(text, 5)); try std.testing.expectEqual(@as(usize, 5), rawAtDisplay(text, 3)); + // At the line's end and past it (virtual columns, one byte each). + try std.testing.expectEqual(text.len, rawAtDisplay(text, 5)); + try std.testing.expectEqual(text.len + 2, rawAtDisplay(text, 7)); try std.testing.expectEqual(@as(usize, 2), graphemeDisplayWidth("👩\u{200d}🚀")); } @@ -2684,7 +2656,6 @@ const OutputTests = struct { const max_arg = panes.Output.max_arg; const State = panes.Output.State; const setArg = panes.Output.setArg; - const fromWord = panes.Output.fromWord; const fillResults = panes.Output.fillResults; test "Locations align Unicode paths by display width and preserve source indentation" { @@ -2868,7 +2839,7 @@ const OutputTests = struct { if (w.line == 0 and w.pad > 0 and w.at < file.location_rows[0].location_end) break vr; } else return error.MissingWrappedLocation; const w = result.wrapAt(@intCast(wrapped)); - const shown = try panes.File.bodyText(frame.allocator(), result, file, true); + const shown = try panes.File.bodyText(frame.allocator(), result, file); const shown_line = modal.lineSlice(shown, wrapped); try std.testing.expect(std.mem.startsWith(u8, shown_line[panes.File.gutterWidth(result)..], "│ │ ")); const rect = p.rects[result_id]; @@ -3207,12 +3178,6 @@ const OutputTests = struct { try std.testing.expect(!result.body.vsel.active and !result.body.msel.active); } - test "dump origins accept only builtins that actually own output panes" { - try std.testing.expectEqual(Origin{ .cmd = .Help }, fromWord("Help").?); - try std.testing.expect(fromWord("Kill") == null); - try std.testing.expect(fromWord("Theme") == null); - } - test "result refill identity retains the full bounded argument" { const p = try Pardes.init(std.testing.allocator, .{ .tty_only = true, @@ -3285,7 +3250,6 @@ const OutputTests = struct { const ImageTests = struct { const State = panes.Image.State; - const restore = panes.Image.restore; const toggleGlyphArt = panes.Image.toggleGlyphArt; const togglePalette = panes.Image.togglePalette; const toggleAscii = panes.Image.toggleAscii; @@ -3304,18 +3268,6 @@ const ImageTests = struct { ); } - test "dump restore propagates malformed embedded image bytes" { - const p = try pardes.Pardes.init(std.testing.allocator, .{ .tty_only = true }); - defer p.deinit(); - try std.testing.expect(p.panes[1] == null); - try std.testing.expectError(error.InvalidCharacter, restore(p, 1, .{ - .kind = .image, - .tag = "img /tmp/bad.ppm", - .body = "", - .image = .{ .path = "/tmp/bad.ppm", .bytes_b64 = "A..A" }, - })); - try std.testing.expect(p.panes[1] == null); - } }; const PdfTests = struct { @@ -4894,6 +4846,7 @@ test "Zig keywords keep every rendered byte colored across line-number widths" { p.settings.colors = true; p.settings.wrap = view.wrap; p.presentation.enabled = false; + p.sync(); var frame = std.heap.ArenaAllocator.init(gpa); defer frame.deinit(); const surface = try p.render(frame.allocator()); diff --git a/test/pdf.zig b/test/pdf.zig index 3f84195c..3bb751e2 100644 --- a/test/pdf.zig +++ b/test/pdf.zig @@ -278,45 +278,6 @@ test "PDF feature gates keep argv and builtin behavior coherent" { } } -test "PDF dump fallback remains a byte-preserving file" { - const gpa = std.testing.allocator; - const path = "/definitely/missing/pardes-dump-fallback.PDF"; - const source = "%PDF embedded fallback bytes\x00\xff"; - const encoded = try dump.encodeBytes(gpa, source); - defer gpa.free(encoded); - const ids = [_]usize{0}; - const columns = [_]dump.Column{.{ .panes = &ids }}; - const saved_panes = [_]dump.Pane{.{ - .kind = .image, - .tag = "pdf 3/9 height PdfFit full PdfTint PdfSections " ++ path ++ " Keep Del", - .tag_tail = " Keep Del", - .body = "", - .scroll = 2, - .cols = 80, - .rows = 24, - .image = .{ .path = path, .bytes_b64 = encoded }, - }}; - const state = dump.State{ - .screen = .{ .cols = 80, .rows = 24 }, - .columns = &columns, - .panes = &saved_panes, - }; - var out: std.Io.Writer.Allocating = .init(gpa); - defer out.deinit(); - try std.zon.stringify.serialize(state, .{ .whitespace = true }, &out.writer); - - const restored = try pardes.dump.initFromDump(gpa, .{ .tty_only = true }, out.written()); - defer restored.deinit(); - const pane = restored.panes[0].?; - try std.testing.expect(pane.file != null); - try std.testing.expect(pane.image == null); - try std.testing.expect(!hasPdf(pane)); - try std.testing.expectEqualStrings(path, pane.file.?.path); - try std.testing.expectEqualSlices(u8, source, pane.file.?.content); - try std.testing.expect(pane.tag.own != null); - try std.testing.expectEqualStrings(" Keep Del", pane.tag.own.?); -} - test "a +PdfSections row's first number is the page: a section not there, or on another page, is a miss said" { if (!pdf_enabled or platform == .web) return; const gpa = std.testing.allocator; @@ -1454,7 +1415,7 @@ test "PDF normal adapter consumes unsupported actions and navigates page fallbac try std.testing.expect(pane.prompt == .none); } -test "Escape cancels PDF chrome in place and Shift-Escape leaves the pane" { +test "Escape cancels PDF chrome in place, then leaves the pane; Shift-Escape leaves at once" { if (!pdf_enabled or platform == .web) return; const gpa = std.testing.allocator; @@ -1507,15 +1468,32 @@ test "Escape cancels PDF chrome in place and Shift-Escape leaves the pane" { try std.testing.expectEqual(scroll_before, pv.document_scroll_y); try std.testing.expectEqual(@as(u32, 0), pane.body.normal.count); try std.testing.expectEqual(.none, pane.body.normal.prefix); - // A second Escape on a bare document is inert rather than an exit. + // With nothing left to drop, Escape goes back a pane as anywhere else, + // and leaves the document as it found it. p.update(.{ .key = .{ .cp = Key.escape } }); - try std.testing.expectEqual(@as(usize, 0), p.active); - - // Shift-Escape is the way out, and it leaves the document as it found it. - p.update(.{ .key = .{ .cp = Key.escape, .shift = true } }); try std.testing.expectEqual(@as(usize, 1), p.active); try std.testing.expectEqual(page_before, pv.page); try std.testing.expectEqual(scroll_before, pv.document_scroll_y); + + // Shift-Escape goes at once, over a selection too. + p.update(.{ .key = .{ .cp = Key.escape } }); + try std.testing.expectEqual(@as(usize, 0), p.active); + try std.testing.expectEqual( + panes.Pdf.SelectionUpdate.changed, + pv.setSelection(p.pdf_gpa, quad.ul, quad.lr, false), + ); + p.update(.{ .key = .{ .cp = Key.escape, .shift = true } }); + try std.testing.expectEqual(@as(usize, 1), p.active); + try std.testing.expect(pv.selection != null); + + // A text-only page shows neither: one Escape drops them and goes. + p.native_images = false; + p.update(.{ .key = .{ .cp = Key.escape } }); + try std.testing.expectEqual(@as(usize, 0), p.active); + try pv.setSearchQuery(p.pdf_gpa, "Pardes"); + p.update(.{ .key = .{ .cp = Key.escape } }); + try std.testing.expectEqual(@as(usize, 1), p.active); + try std.testing.expect(pv.selection == null and pv.search_query.len == 0); } test "PDF continuous strip renders every intersecting short page" { diff --git a/test/perf-baseline-gui-debug.json b/test/perf-baseline-gui-debug.json index a41c92b2..4f97a834 100644 --- a/test/perf-baseline-gui-debug.json +++ b/test/perf-baseline-gui-debug.json @@ -1 +1 @@ -{"harness":"442f274a5da9136c","build":{"compiler":"0.17.0","target":"x86_64-linux.5.10...7.2-gnu.2.38","cpu":"x86_64","driver_optimize":"fast","core_optimize":"debug","c_optimize":"fast","terminal_optimize":"safe","platform":"gui","grammars":"full","mupdf":true,"jpx":true,"tracy":false,"quic":false,"theme_animation":true,"terminal_simd":true,"prebuilt_shaders":false},"cols":120,"rows":40,"reps":10,"only":"medium","fixtures":[{"name":"small","lines":1000,"cols":60,"bytes":62694},{"name":"medium","lines":50000,"cols":60,"bytes":3201944},{"name":"large","lines":300000,"cols":60,"bytes":19464444},{"name":"longline","lines":400,"cols":8000,"bytes":3200400},{"name":"esp32p4","lines":12,"cols":240,"bytes":2892}],"cells":[{"op":"open","fixture":"medium","min_us":2253,"med_us":2626,"p90_us":3065,"max_us":3090},{"op":"render","fixture":"medium","min_us":100,"med_us":103,"p90_us":103,"max_us":110},{"op":"key-down","fixture":"medium","min_us":102,"med_us":103,"p90_us":105,"max_us":113},{"op":"key-right","fixture":"medium","min_us":98,"med_us":104,"p90_us":108,"max_us":113},{"op":"page-down","fixture":"medium","min_us":103,"med_us":104,"p90_us":111,"max_us":114},{"op":"wheel","fixture":"medium","min_us":98,"med_us":100,"p90_us":102,"max_us":108},{"op":"edit-char","fixture":"medium","min_us":1251,"med_us":1940,"p90_us":2652,"max_us":3041},{"op":"edit-twin","fixture":"medium","min_us":2779,"med_us":3177,"p90_us":3366,"max_us":3657},{"op":"body-2m","fixture":"medium","min_us":5812,"med_us":11423,"p90_us":13681,"max_us":13758},{"op":"body-appends","fixture":"medium","min_us":153898,"med_us":241075,"p90_us":284518,"max_us":305761},{"op":"deep-sel","fixture":"medium","min_us":86,"med_us":91,"p90_us":94,"max_us":122}]} +{"harness":"8ecb4caed482b2","build":{"compiler":"0.17.0","target":"x86_64-linux.5.10...7.2-gnu.2.38","cpu":"x86_64","driver_optimize":"fast","core_optimize":"debug","c_optimize":"fast","terminal_optimize":"safe","platform":"gui","grammars":"full","mupdf":true,"jpx":true,"tracy":false,"quic":false,"theme_animation":true,"terminal_simd":true,"prebuilt_shaders":false},"cols":120,"rows":40,"reps":10,"only":"medium","fixtures":[{"name":"small","lines":1000,"cols":60,"bytes":62694},{"name":"medium","lines":50000,"cols":60,"bytes":3201944},{"name":"large","lines":300000,"cols":60,"bytes":19464444},{"name":"longline","lines":400,"cols":8000,"bytes":3200400},{"name":"esp32p4","lines":12,"cols":240,"bytes":2892}],"cells":[{"op":"open","fixture":"medium","min_us":2065,"med_us":2303,"p90_us":2587,"max_us":3268},{"op":"render","fixture":"medium","min_us":90,"med_us":93,"p90_us":96,"max_us":99},{"op":"key-down","fixture":"medium","min_us":99,"med_us":104,"p90_us":114,"max_us":115},{"op":"key-right","fixture":"medium","min_us":100,"med_us":102,"p90_us":104,"max_us":108},{"op":"page-down","fixture":"medium","min_us":98,"med_us":100,"p90_us":114,"max_us":115},{"op":"wheel","fixture":"medium","min_us":95,"med_us":99,"p90_us":105,"max_us":106},{"op":"edit-char","fixture":"medium","min_us":1084,"med_us":1287,"p90_us":1461,"max_us":1655},{"op":"edit-twin","fixture":"medium","min_us":913,"med_us":1303,"p90_us":1531,"max_us":1687},{"op":"body-2m","fixture":"medium","min_us":5120,"med_us":10396,"p90_us":12244,"max_us":13939},{"op":"body-appends","fixture":"medium","min_us":149170,"med_us":235049,"p90_us":263142,"max_us":275060},{"op":"deep-sel","fixture":"medium","min_us":81,"med_us":83,"p90_us":95,"max_us":103}]} diff --git a/test/perf-baseline-tty-debug.json b/test/perf-baseline-tty-debug.json index dc37e033..adc1f23c 100644 --- a/test/perf-baseline-tty-debug.json +++ b/test/perf-baseline-tty-debug.json @@ -1 +1 @@ -{"harness":"442f274a5da9136c","build":{"compiler":"0.17.0","target":"x86_64-linux.5.10...7.2-gnu.2.38","cpu":"x86_64","driver_optimize":"fast","core_optimize":"debug","c_optimize":"fast","terminal_optimize":"safe","platform":"tty","grammars":"full","mupdf":true,"jpx":true,"tracy":false,"quic":false,"theme_animation":true,"terminal_simd":true,"prebuilt_shaders":false},"cols":120,"rows":40,"reps":10,"only":"medium","fixtures":[{"name":"small","lines":1000,"cols":60,"bytes":62694},{"name":"medium","lines":50000,"cols":60,"bytes":3201944},{"name":"large","lines":300000,"cols":60,"bytes":19464444},{"name":"longline","lines":400,"cols":8000,"bytes":3200400},{"name":"esp32p4","lines":12,"cols":240,"bytes":2892}],"cells":[{"op":"open","fixture":"medium","min_us":2181,"med_us":2495,"p90_us":2621,"max_us":2660},{"op":"render","fixture":"medium","min_us":96,"med_us":98,"p90_us":102,"max_us":112},{"op":"key-down","fixture":"medium","min_us":101,"med_us":103,"p90_us":107,"max_us":108},{"op":"key-right","fixture":"medium","min_us":100,"med_us":112,"p90_us":118,"max_us":119},{"op":"page-down","fixture":"medium","min_us":97,"med_us":101,"p90_us":105,"max_us":107},{"op":"wheel","fixture":"medium","min_us":98,"med_us":100,"p90_us":108,"max_us":110},{"op":"edit-char","fixture":"medium","min_us":1377,"med_us":1842,"p90_us":2336,"max_us":2418},{"op":"edit-twin","fixture":"medium","min_us":2655,"med_us":3200,"p90_us":3321,"max_us":3660},{"op":"body-2m","fixture":"medium","min_us":5420,"med_us":10753,"p90_us":12826,"max_us":15385},{"op":"body-appends","fixture":"medium","min_us":153569,"med_us":232519,"p90_us":281625,"max_us":309036},{"op":"deep-sel","fixture":"medium","min_us":91,"med_us":98,"p90_us":111,"max_us":111}]} +{"harness":"8ecb4caed482b2","build":{"compiler":"0.17.0","target":"x86_64-linux.5.10...7.2-gnu.2.38","cpu":"x86_64","driver_optimize":"fast","core_optimize":"debug","c_optimize":"fast","terminal_optimize":"safe","platform":"tty","grammars":"full","mupdf":true,"jpx":true,"tracy":false,"quic":false,"theme_animation":true,"terminal_simd":true,"prebuilt_shaders":false},"cols":120,"rows":40,"reps":10,"only":"medium","fixtures":[{"name":"small","lines":1000,"cols":60,"bytes":62694},{"name":"medium","lines":50000,"cols":60,"bytes":3201944},{"name":"large","lines":300000,"cols":60,"bytes":19464444},{"name":"longline","lines":400,"cols":8000,"bytes":3200400},{"name":"esp32p4","lines":12,"cols":240,"bytes":2892}],"cells":[{"op":"open","fixture":"medium","min_us":2131,"med_us":2550,"p90_us":2652,"max_us":2657},{"op":"render","fixture":"medium","min_us":91,"med_us":95,"p90_us":95,"max_us":98},{"op":"key-down","fixture":"medium","min_us":100,"med_us":101,"p90_us":110,"max_us":114},{"op":"key-right","fixture":"medium","min_us":99,"med_us":103,"p90_us":110,"max_us":116},{"op":"page-down","fixture":"medium","min_us":99,"med_us":102,"p90_us":113,"max_us":114},{"op":"wheel","fixture":"medium","min_us":98,"med_us":100,"p90_us":101,"max_us":115},{"op":"edit-char","fixture":"medium","min_us":1029,"med_us":1242,"p90_us":1379,"max_us":1474},{"op":"edit-twin","fixture":"medium","min_us":1020,"med_us":1348,"p90_us":1453,"max_us":1493},{"op":"body-2m","fixture":"medium","min_us":5544,"med_us":8619,"p90_us":10736,"max_us":12537},{"op":"body-appends","fixture":"medium","min_us":128223,"med_us":186404,"p90_us":230106,"max_us":267921},{"op":"deep-sel","fixture":"medium","min_us":80,"med_us":83,"p90_us":87,"max_us":87}]} diff --git a/test/perf-baseline-tty-fast.json b/test/perf-baseline-tty-fast.json index bd8f32dc..add1b6af 100644 --- a/test/perf-baseline-tty-fast.json +++ b/test/perf-baseline-tty-fast.json @@ -1 +1 @@ -{"harness":"442f274a5da9136c","build":{"compiler":"0.17.0","target":"x86_64-linux.5.10...7.2-gnu.2.38","cpu":"x86_64","driver_optimize":"fast","core_optimize":"fast","c_optimize":"fast","terminal_optimize":"fast","platform":"tty","grammars":"full","mupdf":true,"jpx":true,"tracy":false,"quic":false,"theme_animation":true,"terminal_simd":true,"prebuilt_shaders":false},"cols":120,"rows":40,"reps":10,"only":"medium","fixtures":[{"name":"small","lines":1000,"cols":60,"bytes":62694},{"name":"medium","lines":50000,"cols":60,"bytes":3201944},{"name":"large","lines":300000,"cols":60,"bytes":19464444},{"name":"longline","lines":400,"cols":8000,"bytes":3200400},{"name":"esp32p4","lines":12,"cols":240,"bytes":2892}],"cells":[{"op":"open","fixture":"medium","min_us":2283,"med_us":2611,"p90_us":2812,"max_us":2917},{"op":"render","fixture":"medium","min_us":89,"med_us":97,"p90_us":105,"max_us":119},{"op":"key-down","fixture":"medium","min_us":88,"med_us":95,"p90_us":100,"max_us":116},{"op":"key-right","fixture":"medium","min_us":90,"med_us":99,"p90_us":103,"max_us":106},{"op":"page-down","fixture":"medium","min_us":86,"med_us":90,"p90_us":96,"max_us":97},{"op":"wheel","fixture":"medium","min_us":85,"med_us":91,"p90_us":94,"max_us":101},{"op":"edit-char","fixture":"medium","min_us":1229,"med_us":1823,"p90_us":1874,"max_us":2101},{"op":"edit-twin","fixture":"medium","min_us":2196,"med_us":2745,"p90_us":2928,"max_us":2972},{"op":"body-2m","fixture":"medium","min_us":5606,"med_us":10369,"p90_us":14063,"max_us":14406},{"op":"body-appends","fixture":"medium","min_us":155867,"med_us":237195,"p90_us":289128,"max_us":306283},{"op":"deep-sel","fixture":"medium","min_us":82,"med_us":84,"p90_us":86,"max_us":91}]} +{"harness":"8ecb4caed482b2","build":{"compiler":"0.17.0","target":"x86_64-linux.5.10...7.2-gnu.2.38","cpu":"x86_64","driver_optimize":"fast","core_optimize":"fast","c_optimize":"fast","terminal_optimize":"fast","platform":"tty","grammars":"full","mupdf":true,"jpx":true,"tracy":false,"quic":false,"theme_animation":true,"terminal_simd":true,"prebuilt_shaders":false},"cols":120,"rows":40,"reps":10,"only":"medium","fixtures":[{"name":"small","lines":1000,"cols":60,"bytes":62694},{"name":"medium","lines":50000,"cols":60,"bytes":3201944},{"name":"large","lines":300000,"cols":60,"bytes":19464444},{"name":"longline","lines":400,"cols":8000,"bytes":3200400},{"name":"esp32p4","lines":12,"cols":240,"bytes":2892}],"cells":[{"op":"open","fixture":"medium","min_us":2096,"med_us":2329,"p90_us":2589,"max_us":2704},{"op":"render","fixture":"medium","min_us":77,"med_us":80,"p90_us":85,"max_us":86},{"op":"key-down","fixture":"medium","min_us":84,"med_us":87,"p90_us":101,"max_us":116},{"op":"key-right","fixture":"medium","min_us":84,"med_us":87,"p90_us":93,"max_us":111},{"op":"page-down","fixture":"medium","min_us":85,"med_us":86,"p90_us":89,"max_us":102},{"op":"wheel","fixture":"medium","min_us":83,"med_us":87,"p90_us":98,"max_us":107},{"op":"edit-char","fixture":"medium","min_us":886,"med_us":1127,"p90_us":1466,"max_us":1503},{"op":"edit-twin","fixture":"medium","min_us":928,"med_us":1282,"p90_us":1489,"max_us":1586},{"op":"body-2m","fixture":"medium","min_us":5327,"med_us":8382,"p90_us":10779,"max_us":11879},{"op":"body-appends","fixture":"medium","min_us":140582,"med_us":203008,"p90_us":242154,"max_us":252106},{"op":"deep-sel","fixture":"medium","min_us":71,"med_us":74,"p90_us":75,"max_us":77}]} diff --git a/test/perf.zig b/test/perf.zig index ab284739..31a48cc7 100644 --- a/test/perf.zig +++ b/test/perf.zig @@ -378,6 +378,10 @@ const Options = struct { startup: bool = false, mini: bool = false, terminal_mib: ?usize = null, + /// The real binary's keystroke-to-present, `yes` stream and big open + /// (a tty pardes), or its keystrokes alone (a pardes-gui). + live: ?[]const u8 = null, + live_gui: ?[]const u8 = null, self_test: bool = false, /// A gate: fail when a cell's fastest sample is more than this many /// times the baseline's fastest (--base's). @@ -389,7 +393,7 @@ const Options = struct { while (i < args.len) : (i += 1) { const arg = args[i]; if (!std.mem.startsWith(u8, arg, "--")) return error.UnknownOption; - const option = std.meta.stringToEnum(enum { json, creation, startup, mini, @"terminal-mib", @"self-test", reps, cols, rows, base, only, @"max-ratio" }, arg[2..]) orelse + const option = std.meta.stringToEnum(enum { json, creation, startup, mini, @"terminal-mib", @"self-test", reps, cols, rows, base, only, @"max-ratio", live, @"live-gui" }, arg[2..]) orelse return error.UnknownOption; switch (option) { .json => opts.json = true, @@ -409,6 +413,8 @@ const Options = struct { .only => opts.only = value, .@"terminal-mib" => opts.terminal_mib = std.fmt.parseInt(usize, value, 10) catch return error.InvalidNumber, .@"max-ratio" => opts.max_ratio = std.fmt.parseFloat(f64, value) catch return error.InvalidNumber, + .live => opts.live = value, + .@"live-gui" => opts.live_gui = value, else => unreachable, } }, @@ -452,6 +458,8 @@ pub fn main(init: std.process.Init) !void { if (opts.creation) return measureCreation(init.io, reps, opts.json); if (opts.terminal_mib) |mib| return measureTerminalSession(init.io, mib, reps, opts.json); if (opts.mini) return measureMini(init.io, init.gpa, reps, opts.json); + if (opts.live) |exe| return measureLive(init.io, exe, false, reps, opts.json); + if (opts.live_gui) |exe| return measureLive(init.io, exe, true, reps, opts.json); var random: [12]u8 = undefined; init.io.random(&random); @@ -1067,6 +1075,348 @@ fn measureTerminalSession(io: std.Io, mib: usize, reps: usize, json: bool) !void try std.Io.File.stdout().writeStreamingAll(io, "\n"); } +// ---- live: the real binary, in a pty (tty) or a hidden test window (gui) ---- +// +// Keystroke-to-present is FrameStats' own latency: from the shell taking an +// input off its queue to the end of the frame's present. Both frontends run +// FrameStats.keys (the tty through the pty, the gui as its timedemo) and the +// frames' dump (PARDES_FRAMES) is split by its phases. `yes | head -c 32M` is +// typed into a shell and timed to the mark after it; the big file is opened +// on the command line and timed to its first line on screen. + +const eh = @import("e2e_harness.zig"); +extern "c" fn setenv(name: [*:0]const u8, value: [*:0]const u8, overwrite: c_int) c_int; + +const live_phases = [_][]const u8{ "key-down", "key-right", "edit-char", "page-down", "wheel" }; +const yes_bytes = 32 << 20; +const big_lines = 1_000_000; + +const Live = struct { + dir: []const u8, + /// Each phase's frame latencies, µs, over every run, and the core's + /// part of those frames (update and render: the rest is the shell's + /// present, a test window's capture of the frame included). + samples: [live_phases.len]std.ArrayList(u32) = @splat(.empty), + core: [live_phases.len]std.ArrayList(u32) = @splat(.empty), + yes_ns: std.ArrayList(u64) = .empty, + yes_frames: std.ArrayList(u64) = .empty, + open_ns: std.ArrayList(u64) = .empty, + + fn path(l: *const Live, comptime name: []const u8) ![:0]const u8 { + return std.fmt.allocPrintSentinel(gpa, "{s}/" ++ name, .{l.dir}, 0); + } +}; + +/// A private home (bash with a `$ ` prompt, as the snapshot harness pins), +/// runtime directory and the two fixtures, under $TMPDIR. +fn liveSetup(io: std.Io) !Live { + var random: [8]u8 = undefined; + io.random(&random); + const tmp = if (std.c.getenv("TMPDIR")) |t| std.mem.span(t) else "/tmp"; + const dir = try std.fmt.allocPrint(gpa, "{s}/pardes-perf-live-{x}", .{ tmp, random }); + try std.Io.Dir.cwd().createDirPath(io, dir); + const l: Live = .{ .dir = dir }; + try std.Io.Dir.cwd().createDirPath(io, try l.path("home/.config/pardes")); + try writeFile(try l.path("home/.bashrc"), "PS1='$ '\nHISTFILE=\n"); + try writeFile(try l.path("home/.config/pardes/init"), "Shell bash\n"); + // A runtime directory of its own: no session of these shows up in the + // user's. Short, for the 9P socket's 108-byte path. + const run = try std.fmt.allocPrintSentinel(gpa, "/tmp/pardes-perf-run-{x}", .{random}, 0); + try std.Io.Dir.cwd().createDirPath(io, run); + _ = setenv("XDG_RUNTIME_DIR", run, 1); + _ = setenv("HOME", try l.path("home"), 1); + _ = setenv("XDG_CONFIG_HOME", try l.path("home/.config"), 1); + _ = setenv("TERM", "xterm-256color", 1); + const medium = try generate(fixtures[1]); + defer gpa.free(medium); + try writeFile(try l.path("medium.zig"), medium); + var big: std.ArrayList(u8) = .empty; + defer big.deinit(gpa); + for (0..big_lines) |n| try big.print(gpa, "line {d:0>7} the quick brown fox jumps over the lazy dog 0123456789\n", .{n + 1}); + try writeFile(try l.path("big.txt"), big.items); + return l; +} + +fn liveCleanup(io: std.Io, l: *Live) void { + liveSweep(io); + std.Io.Dir.cwd().deleteTree(io, l.dir) catch {}; + if (std.c.getenv("XDG_RUNTIME_DIR")) |run| std.Io.Dir.cwd().deleteTree(io, std.mem.span(run)) catch {}; +} + +/// Every process a live run started that is still there, ended: a tty stub +/// killed since phase 3 detaches its session rather than ending it, and an +/// exe that is a wrapper may start sessions of its own (`--detach`). Each +/// inherited the run's private runtime directory, which no other process +/// has: TERM, then KILL what is left. After every cell, and in the cleanup +/// every path takes. +fn liveSweep(io: std.Io) void { + const run = std.mem.span(std.c.getenv("XDG_RUNTIME_DIR") orelse return); + var want_buf: [512]u8 = undefined; + const want = std.fmt.bufPrint(&want_buf, "XDG_RUNTIME_DIR={s}", .{run}) catch return; + const env = gpa.alloc(u8, 1 << 20) catch return; + defer gpa.free(env); + const self = libc.getpid(); + for ([_]@TypeOf(libc.SIG.TERM){ .TERM, .KILL }) |sig| { + var left: usize = 0; + var proc = std.Io.Dir.cwd().openDir(io, "/proc", .{ .iterate = true }) catch return; + defer proc.close(io); + var it = proc.iterate(); + while (it.next(io) catch null) |entry| { + const pid = std.fmt.parseInt(libc.pid_t, entry.name, 10) catch continue; + if (pid == self) continue; + var path_buf: [64]u8 = undefined; + const path = std.fmt.bufPrintSentinel(&path_buf, "/proc/{d}/environ", .{pid}, 0) catch continue; + const fd = libc.open(path, .{ .ACCMODE = .RDONLY }); + if (fd < 0) continue; + var n: usize = 0; + while (n < env.len) { + const got = libc.read(fd, env.ptr + n, env.len - n); + if (got <= 0) break; + n += @intCast(got); + } + _ = libc.close(fd); + var vars = std.mem.splitScalar(u8, env[0..n], 0); + while (vars.next()) |v| if (std.mem.eql(u8, v, want)) { + _ = libc.kill(pid, sig); + left += 1; + break; + }; + } + if (left == 0) return; + // A session ends on TERM (it detaches nothing then): a moment for it. + if (sig == .TERM) io.sleep(.fromMilliseconds(300), .awake) catch {}; + } +} + +/// The bytes a terminal sends for a step of FrameStats.keys. +fn stepBytes(buf: *[32]u8, step: pardes.FrameStats.Step) []const u8 { + return switch (step.act) { + .key => |k| switch (k.cp) { + pardes.Key.escape => "\x1b", + pardes.Key.page_down => "\x1b[6~", + else => buf[0 .. std.unicode.utf8Encode(k.cp, buf) catch 0], + }, + // SGR wheel down (65) or up (64), over the file's body. + .wheel => |notches| std.fmt.bufPrint(buf, "\x1b[<{d};60;20M", .{@as(u8, if (notches > 0) 65 else 64)}) catch "", + else => "", + }; +} + +/// Feeds the harness until the app exits (reaped) or `ms` runs out (killed, +/// its screen shown). +fn liveExit(h: *eh.Harness, ms: i64) bool { + const deadline = eh.nowMs() + ms; + while (eh.nowMs() < deadline) { + var status: c_int = 0; + if (libc.waitpid(h.pid, &status, libc.W.NOHANG) == h.pid) { + if (status != 0) h.dump("live: the app failed"); + // Reaped: the harness must not signal a pid that is gone. + _ = libc.close(h.master); + h.stream.deinit(); + gpa.destroy(h.raw); + h.term.deinit(gpa); + gpa.destroy(h.term); + return true; + } + _ = h.pumpOnce(20) catch false; + } + h.dump("live: the app did not exit"); + h.deinit(); + _ = libc.waitpid(h.pid, null, 0); + return false; +} + +/// Exit, and Exit again past the question an edited file asks. +fn liveQuit(h: *eh.Harness) !bool { + try h.send("\x1b"); + try h.pump(60); + try h.send(" q"); + try h.pump(200); + try h.send(" q"); + return liveExit(h, 10_000); +} + +/// How many frames a PARDES_FRAMES dump says were drawn. +fn dumpFrames(dump_path: []const u8) !u64 { + const text = try readFileAlloc(dump_path); + defer gpa.free(text); + if (!std.mem.startsWith(u8, text, "# frames ")) return error.BadFrames; + var it = std.mem.tokenizeScalar(u8, text["# frames ".len..], ' '); + return std.fmt.parseInt(u64, it.next() orelse return error.BadFrames, 10); +} + +/// The frames of a PARDES_FRAMES dump, each latency into the phase it fell +/// in. `starts` are the phases' monotonic starts, or null to take them +/// from the dump's own `# phase` lines (the GUI's timedemo writes them). +fn liveFrames(l: *Live, dump_path: []const u8, starts: ?*const [live_phases.len + 1]u64) !void { + const text = try readFileAlloc(dump_path); + defer gpa.free(text); + var t0: u64 = 0; + // Each phase's start and, last, "done"'s: the quit's keys are no phase's. + var from: [live_phases.len + 1]u64 = if (starts) |s| s.* else @splat(std.math.maxInt(u64)); + var lines = std.mem.splitScalar(u8, text, '\n'); + while (lines.next()) |line| { + if (std.mem.startsWith(u8, line, "# t0_ns ")) { + t0 = try std.fmt.parseInt(u64, line["# t0_ns ".len..], 10); + } else if (std.mem.startsWith(u8, line, "# phase ") and starts == null) { + var it = std.mem.tokenizeScalar(u8, line["# phase ".len..], ' '); + const name = it.next() orelse continue; + const at = t0 + @as(u64, @intFromFloat(try std.fmt.parseFloat(f64, it.next() orelse continue) * std.time.ns_per_ms)); + for (live_phases ++ [_][]const u8{"done"}, 0..) |phase, i| if (std.mem.eql(u8, name, phase)) { + from[i] = at; + }; + } + } + lines = std.mem.splitScalar(u8, text, '\n'); + while (lines.next()) |line| { + if (line.len == 0 or line[0] == '#') continue; + var fields = std.mem.splitScalar(u8, line, '\t'); + const start_ms = try std.fmt.parseFloat(f64, fields.next() orelse continue); + const update = try std.fmt.parseInt(u32, fields.next() orelse continue, 10); + const render = try std.fmt.parseInt(u32, fields.next() orelse continue, 10); + for (0..6) |_| _ = fields.next(); + const latency = try std.fmt.parseInt(u32, fields.next() orelse continue, 10); + if (latency == 0) continue; + const at = t0 + @as(u64, @intFromFloat(start_ms * std.time.ns_per_ms)); + var phase: ?usize = null; + for (from, 0..) |f, i| if (at >= f) { + phase = i; + }; + if (phase) |i| if (i < live_phases.len) { + try l.samples[i].append(gpa, latency); + try l.core[i].append(gpa, update + render); + }; + } +} + +fn liveKeysTty(l: *Live, exe: [:0]const u8) !void { + const dump_path = try l.path("tty-frames.tsv"); + _ = setenv("PARDES_FRAMES", dump_path, 1); + var h = try eh.Harness.initArgv(gpa, exe, screen_rows, screen_cols, &.{(try l.path("medium.zig")).ptr}); + if (!try h.waitFor("helper_1", 20_000)) return error.LiveFileNotShown; + try h.pump(300); + var starts: [live_phases.len + 1]u64 = @splat(std.math.maxInt(u64)); + const t0 = nowNs(); + var buf: [32]u8 = undefined; + for (pardes.FrameStats.named("keys").?) |step| { + const due = t0 + @as(u64, step.at_ms) * std.time.ns_per_ms; + while (nowNs() < due) _ = try h.pumpOnce(@intCast(@max(1, (due -| nowNs()) / std.time.ns_per_ms))); + switch (step.act) { + .phase => |name| for (live_phases ++ [_][]const u8{"done"}, 0..) |phase, i| if (std.mem.eql(u8, name, phase)) { + starts[i] = nowNs(); + }, + .end => break, + else => try h.send(stepBytes(&buf, step)), + } + } + if (!try liveQuit(&h)) return error.LiveDidNotExit; + try liveFrames(l, dump_path, &starts); +} + +fn liveYes(l: *Live, exe: [:0]const u8) !void { + const dump_path = try l.path("yes-frames.tsv"); + _ = setenv("PARDES_FRAMES", dump_path, 1); + var h = try eh.Harness.initArgv(gpa, exe, screen_rows, screen_cols, &.{ "-n", "1" }); + errdefer h.dump("live yes"); + // The prompt shows in tty mode only: the shell's tag, then Ctrl-b in. + if (!try h.waitFor("Tty+bash", 20_000)) return error.LiveNoPrompt; + try h.pump(500); + try h.send("\x02"); + if (!try h.waitFor("$ ", 20_000)) return error.LiveNoPrompt; + try h.send(std.fmt.comptimePrint("yes | head -c {d}; echo DO''NE", .{yes_bytes})); + try h.pump(100); + const t0 = nowNs(); + try h.send("\r"); + if (!try h.waitFor("DONE", 600_000)) return error.LiveYesUnfinished; + try l.yes_ns.append(gpa, nowNs() - t0); + try h.pump(200); + try h.send("exit\r"); + if (!liveExit(&h, 10_000)) return error.LiveDidNotExit; + // Only the frame count: a stream's frames answer no input. + try l.yes_frames.append(gpa, try dumpFrames(dump_path)); +} + +fn liveOpen(l: *Live, exe: [:0]const u8) !void { + _ = setenv("PARDES_FRAMES", "", 1); + const t0 = nowNs(); + var h = try eh.Harness.initArgv(gpa, exe, screen_rows, screen_cols, &.{(try l.path("big.txt")).ptr}); + if (!try h.waitFor("line 0000001", 60_000)) return error.LiveFileNotShown; + try l.open_ns.append(gpa, nowNs() - t0); + if (!try liveQuit(&h)) return error.LiveDidNotExit; +} + +fn liveKeysGui(l: *Live, exe: [:0]const u8) !void { + const dump_path = try l.path("gui-frames.tsv"); + _ = setenv("PARDES_FRAMES", dump_path, 1); + _ = setenv("PARDES_FRAMES_DEMO", "keys", 1); + _ = setenv("PARDES_TEST", "1", 1); + _ = setenv("PARDES_TEST_CAPTURE_DIR", try std.fmt.allocPrintSentinel(gpa, "{s}", .{l.dir}, 0), 1); + _ = setenv("PARDES_TEST_COLS", try std.fmt.allocPrintSentinel(gpa, "{d}", .{screen_cols}, 0), 1); + _ = setenv("PARDES_TEST_ROWS", try std.fmt.allocPrintSentinel(gpa, "{d}", .{screen_rows}, 0), 1); + _ = setenv("SDL_VIDEODRIVER", "wayland", 1); + // The private runtime directory has no display: the user's, by path. + const display = std.mem.span(std.c.getenv("WAYLAND_DISPLAY") orelse "wayland-0"); + if (display.len > 0 and display[0] != '/') + _ = setenv("WAYLAND_DISPLAY", try std.fmt.allocPrintSentinel(gpa, "/run/user/{d}/{s}", .{ std.c.getuid(), display }, 0), 1); + // Under the pty only for a stdin that stays open (test mode reads it). + var h = try eh.Harness.initArgv(gpa, exe, screen_rows, screen_cols, &.{(try l.path("medium.zig")).ptr}); + if (!liveExit(&h, 120_000)) return error.LiveDidNotExit; + try liveFrames(l, dump_path, null); +} + +/// p50 and p99, sorting `s`. +fn spread(s: []u32) [2]u32 { + std.mem.sort(u32, s, {}, std.sort.asc(u32)); + return .{ s[s.len / 2], s[@min(s.len - 1, s.len * 99 / 100)] }; +} + +fn measureLive(io: std.Io, exe: []const u8, gui: bool, reps: usize, json: bool) !void { + var l = try liveSetup(io); + defer liveCleanup(io, &l); + const exe_z = try gpa.dupeSentinel(u8, exe, 0); + for (0..reps) |_| { + if (gui) { + try liveKeysGui(&l, exe_z); + liveSweep(io); + continue; + } + try liveKeysTty(&l, exe_z); + liveSweep(io); + try liveYes(&l, exe_z); + liveSweep(io); + try liveOpen(&l, exe_z); + liveSweep(io); + } + const frontend = if (gui) "gui" else "tty"; + const out = std.Io.File.stdout(); + var buf: [256]u8 = undefined; + for (live_phases, &l.samples, &l.core) |name, *samples, *core| { + if (samples.items.len == 0) return error.LivePhaseUnmeasured; + const all = spread(samples.items); + const own = spread(core.items); + const line = if (json) + try std.fmt.bufPrint(&buf, "{{\"case\":\"live-{s}-{s}\",\"n\":{d},\"p50_us\":{d},\"p99_us\":{d},\"core_p50_us\":{d},\"core_p99_us\":{d}}}\n", .{ frontend, name, samples.items.len, all[0], all[1], own[0], own[1] }) + else + try std.fmt.bufPrint(&buf, "live {s} {s:<10} keystroke-to-present n {d:>4} p50 {d:>6} us p99 {d:>6} us, core p50 {d:>6} us p99 {d:>6} us\n", .{ frontend, name, samples.items.len, all[0], all[1], own[0], own[1] }); + try out.writeStreamingAll(io, line); + } + if (gui) return; + for (l.yes_ns.items, l.yes_frames.items) |ns, frames| { + const line = if (json) + try std.fmt.bufPrint(&buf, "{{\"case\":\"live-tty-yes-32m\",\"wall_ns\":{d},\"frames\":{d}}}\n", .{ ns, frames }) + else + try std.fmt.bufPrint(&buf, "live tty yes|head -c 32M: {d} ms wall, {d} frames\n", .{ ns / std.time.ns_per_ms, frames }); + try out.writeStreamingAll(io, line); + } + for (l.open_ns.items) |ns| { + const line = if (json) + try std.fmt.bufPrint(&buf, "{{\"case\":\"live-tty-open-big\",\"lines\":{d},\"wall_ns\":{d}}}\n", .{ big_lines, ns }) + else + try std.fmt.bufPrint(&buf, "live tty open {d}-line file: {d} ms to its first line\n", .{ big_lines, ns / std.time.ns_per_ms }); + try out.writeStreamingAll(io, line); + } +} + const MiniProbe = enum { generate, highlight_generate, render }; const MiniMeasurement = struct { cold_ns: u64 = 0, diff --git a/test/replicas.zig b/test/replicas.zig new file mode 100644 index 00000000..18060763 --- /dev/null +++ b/test/replicas.zig @@ -0,0 +1,1293 @@ +//! pardes-replicas: one authority and several replicas of a session in one +//! process (src/replica.zig), each client typing at once over its own +//! delayed, jittered link, as monkey.zig's scripts type at a tty, each in a +//! view of its own (src/views.zig) over the text they share; `--shared` +//! puts them all in one, where they clash at every key: a stress of +//! correctness, not a picture of a session. +//! +//! Terminals cross as screen state (src/screen_state.zig): the session's +//! shell pane runs a full-screen program and then a `yes` flood (`--flood` +//! MB of it) while the clients type, its output stepped by the authority +//! and sent to each replica as deltas, once a frame and before any input +//! the authority steps, each replica acknowledging them over its link and +//! asking for the rows it scrolls back into and does not hold. +//! +//! zig build replicas -Dplatform=tty -- [--seeds N] [--steps M] [--clients C] [--rtt 30,80,207] [--first K] [--flood MB] [--shared] +//! zig build replicas -Dplatform=tty -- --echo [--seeds N] [--rtt 30,80,207] [--first K] +//! +//! `--echo` types a corpus into a shell instead, with and without the +//! predicted echo (src/echo.zig), and gates what it draws: below, at +//! "predicted echo". +//! +//! Gates, per seed and RTT: no replica diverges (every confirmed unit's +//! digests agree as it is replayed), and at quiescence every replica's +//! digest is the authority's; every delta a replica applies leaves its +//! terminal what the authority's was at that version, and at quiescence it +//! is the authority's, cell for cell. Reported per RTT: how inputs went +//! (applied at once, stopped at a host call, left to the authority), how +//! units were taken (fast path, rollback for a misprediction or another's +//! input), what a speculative apply, a copy and a rollback cost against a +//! frame, and the terminals': deltas a second, wire bytes a second under +//! the flood, encode a frame (every peer), apply a delta. +const std = @import("std"); +const libc = std.c; +const pardes = @import("pardes"); +const replica = pardes.replica; +const screen_state = pardes.screen_state; +const echo = pardes.echo; +const Key = pardes.Key; + +const gpa = std.heap.c_allocator; +var trace = false; +const frame_us = 16_667; + +/// The authority's host: no frames drawn (a replica draws its own), and a +/// pty that echoes what it is written, 2 ms later, as a shell's. +const Host = struct { + echo: std.ArrayListUnmanaged(struct { pane: u8, bytes: []u8 }) = .empty, + + fn watched(_: ?*anyopaque) bool { + return false; + } + + fn ptyWrite(ctx: ?*anyopaque, pane: u8, bytes: []const u8) void { + const h: *Host = @ptrCast(@alignCast(ctx.?)); + h.echo.append(gpa, .{ .pane = pane, .bytes = gpa.dupe(u8, bytes) catch return }) catch {}; + } + + fn ignore(_: ?*anyopaque, _: []const u8) void {} + + const vtable: pardes.Host.VTable = .{ .watched = watched, .pty_write = ptyWrite, .set_clipboard = ignore }; + + fn host(h: *Host) pardes.Host { + return .{ .ctx = h, .vtable = &vtable }; + } +}; + +const Kind = enum { input, to_authority, to_replica, echo, frame, output, screen_down, screen_up, client_frame }; + +const Msg = struct { + at: u64, + n: u64, + kind: Kind, + who: u16, + bytes: []u8, + /// An echo's: the client's input that made it (--echo). + seq: u32 = 0, + + fn order(_: void, a: Msg, b: Msg) std.math.Order { + if (a.at != b.at) return std.math.order(a.at, b.at); + return std.math.order(a.n, b.n); + } +}; + +const Link = struct { + last: u64 = 0, + /// When a message sent at `now` arrives: half the RTT, jitter on top, + /// and never before the one sent ahead of it (a stream keeps order). + fn arrive(l: *Link, random: std.Random, now: u64, one_way: u64, jitter: u64) u64 { + const at = @max(l.last + 1, now + one_way + random.uintLessThan(u64, jitter + 1)); + l.last = at; + return at; + } +}; + +const Totals = struct { + inputs: u64 = 0, + speculated: u64 = 0, + stopped: u64 = 0, + unspeculated: u64 = 0, + units: u64 = 0, + fast: u64 = 0, + mispredicted: u64 = 0, + foreign: u64 = 0, + rebases: u64 = 0, + copies: u64 = 0, + copy_bytes: u64 = 0, + divergences: u64 = 0, + unequal: u64 = 0, + spec_ns: std.ArrayListUnmanaged(u64) = .empty, + copy_ns: std.ArrayListUnmanaged(u64) = .empty, + rollback_ns: std.ArrayListUnmanaged(u64) = .empty, + // Terminals. + apply_ns: std.ArrayListUnmanaged(u64) = .empty, + frame_ns: std.ArrayListUnmanaged(u64) = .empty, + deltas: u64 = 0, + delta_bytes: u64 = 0, + unit_bytes: u64 = 0, + /// Deltas the replicas applied while the flood ran. + flood_deltas: u64 = 0, + flood_us: u64 = 0, + flood_delta_bytes: u64 = 0, + flood_unit_bytes: u64 = 0, + fetched: u64 = 0, + /// Bytes of rows sent before a step's unit, the step having read them. + filled_bytes: u64 = 0, + resets: u64 = 0, + screens_wrong: u64 = 0, + screens_checked: u64 = 0, + + fn add(t: *Totals, s: *const replica.Stats) !void { + t.inputs += s.inputs; + t.speculated += s.speculated; + t.stopped += s.stopped; + t.unspeculated += s.unspeculated; + t.units += s.units; + t.fast += s.fast; + t.mispredicted += s.mispredicted; + t.foreign += s.foreign; + t.rebases += s.rebases; + t.copies += s.copies; + t.copy_bytes += s.copy_bytes; + try t.spec_ns.appendSlice(gpa, s.spec_ns.items); + try t.copy_ns.appendSlice(gpa, s.copy_ns.items); + try t.rollback_ns.appendSlice(gpa, s.rollback_ns.items); + try t.apply_ns.appendSlice(gpa, s.apply_ns.items); + } +}; + +/// One client's next input: monkey.zig's mix, as events, and now and then +/// a wheel over the shell pane, which scrolls its history into view; a +/// file looked at (`looks`, its words) and a pane closed, the Panes and +/// Recent pickers (the store's and Recent's rows, the authority's), a jump. +fn nextEvent(random: std.Random, buf: *[64]u8, wheel_at: ?[2]u16, looks: []const []const u8) pardes.Event { + const typed = "abcdefghijklmnopqrstuvwxyz0123456789 -_=+,:;()"; + const normal = "hjklwbe0$xpuyoOiaAIdcv.nN%sC"; + switch (random.uintLessThan(u8, 43)) { + 0...15 => { + buf[0] = typed[random.uintLessThan(usize, typed.len)]; + return .{ .key = .{ .cp = buf[0], .text = buf[0..1] } }; + }, + 16...27 => { + buf[0] = normal[random.uintLessThan(usize, normal.len)]; + return .{ .key = .{ .cp = buf[0], .text = buf[0..1] } }; + }, + 28...31 => return .{ .key = .{ .cp = Key.escape } }, + 32 => return .{ .key = .{ .cp = Key.enter } }, + 33 => return .{ .key = .{ .cp = Key.backspace } }, + 34 => return .{ .key = .{ .cp = switch (random.uintLessThan(u8, 4)) { + 0 => Key.up, + 1 => Key.down, + 2 => Key.left, + else => Key.right, + } } }, + 35 => { + const n = random.intRangeAtMost(usize, 1, 24); + for (buf[0..n]) |*c| c.* = typed[random.uintLessThan(usize, typed.len)]; + return .{ .paste = buf[0..n] }; + }, + 36 => return .{ .command = switch (random.uintLessThan(u8, 10)) { + 0 => "Newcol", + 1 => "Zerox", + 2 => "Undo", + 3 => "Redo", + 4 => "Search Panes", + 5 => "Search Recent", + 6, 7 => looks[random.uintLessThan(usize, looks.len)], + 8 => "Del", + else => return .{ .key = .{ .cp = if (random.boolean()) 'o' else 'i', .ctrl = true } }, + } }, + 37 => return .{ .resize = .{ .cols = @intCast(random.intRangeAtMost(u16, 60, 120)), .rows = @intCast(random.intRangeAtMost(u16, 16, 40)) } }, + 38 => return .{ .mouse = .{ .button = .left, .kind = .press, .col = random.uintLessThan(u16, 80), .row = random.uintLessThan(u16, 24) } }, + 39 => return .{ .mouse = .{ .button = .left, .kind = .release, .col = random.uintLessThan(u16, 80), .row = random.uintLessThan(u16, 24) } }, + // Out of a shell's tty mode and back: its text, a cursor and + // selections over it. + 40 => return .{ .key = .{ .cp = 'b', .ctrl = true } }, + else => { + const at = wheel_at orelse return .{ .key = .{ .cp = Key.escape } }; + return .{ .mouse = .{ .button = if (random.uintLessThan(u8, 3) == 0) .wheel_down else .wheel_up, .kind = .press, .col = at[0], .row = at[1] } }; + }, + } +} + +fn writeFile(path: [:0]const u8, lines: usize) !void { + const fd = libc.open(path, .{ .ACCMODE = .WRONLY, .CREAT = true, .TRUNC = true }, @as(libc.mode_t, 0o644)); + if (fd < 0) return error.OpenFailed; + defer _ = libc.close(fd); + var line: [80]u8 = undefined; + for (0..lines) |n| { + const text = try std.fmt.bufPrint(&line, "const value_{d:0>4} = {d}; // a line to edit\n", .{ n, n *% 7919 }); + if (libc.write(fd, text.ptr, text.len) != @as(isize, @intCast(text.len))) return error.WriteFailed; + } +} + +const Run = struct { seed: u64, rtt_ms: u64, steps: usize, clients: u16, shared: bool, flood_mb: u64 }; + +/// The terminal pane's screen as a hash of screen_state's dump of it. +fn screenHash(t: *pardes.panes.terminal.VtSlot) u64 { + var buf: [8192]u8 = undefined; + var h: std.Io.Writer.Hashing(std.hash.Wyhash) = .initHasher(.init(0), &buf); + screen_state.dumpScreen(t, &h.writer) catch {}; + h.writer.flush() catch {}; + return h.hasher.final(); +} + +fn run(r: Run, dir: []const u8, totals: *Totals) !void { + if (trace) std.debug.print("seed {d} rtt {d} ms\n", .{ r.seed, r.rtt_ms }); + var prng = std.Random.DefaultPrng.init(r.seed *% 0x9e3779b97f4a7c15 +% r.rtt_ms); + const random = prng.random(); + var host: Host = .{}; + defer { + for (host.echo.items) |e| gpa.free(e.bytes); + host.echo.deinit(gpa); + } + var paths: [3][256:0]u8 = undefined; + var look_lines: [3][300]u8 = undefined; + var looks: [3][]const u8 = undefined; + for (&paths, &look_lines, &looks, 0..) |*p, *l, *look, i| { + const path = try std.fmt.bufPrintSentinel(p, "{s}/f{d}.zig", .{ dir, i }, 0); + try writeFile(path, 60 + 40 * i); + look.* = try std.fmt.bufPrint(l, "Look {s}", .{path}); + } + // The tty's own boot (a raw shell, a text pane under it), then three + // files looked at: keys go to the last, a pane's shell echoes. + const a = try replica.Authority.init(gpa, .{ .cols = 100, .rows = 30, .boot = .tty }, host.host(), true); + defer a.deinit(); + var screens: replica.Screens = .init(gpa); + defer screens.deinit(a.core); + const first = try gpa.dupe(u8, try a.unit(0, 0)); + defer gpa.free(first); + var reps: [8]*replica.Replica = undefined; + var peers: [8]u16 = undefined; + for (reps[0..r.clients], peers[0..r.clients], 1..) |*rp, *peer, id| { + rp.* = try replica.Replica.init(gpa, @intCast(id), first, true); + peer.* = try screens.addPeer(); + } + defer for (reps[0..r.clients]) |rp| rp.deinit(); + const shell: u8 = for (a.core.panes, 0..) |slot, id| { + if (slot) |pane| if (pane.isTerminal()) break @intCast(id); + } else return error.NoShell; + for (&paths) |*path| { + var line: [300]u8 = undefined; + const unit = try a.local(.{ .command = try std.fmt.bufPrint(&line, "Look {s}", .{std.mem.sliceTo(path, 0)}) }); + for (reps[0..r.clients]) |rp| rp.confirm(unit); + } + // Each client a view of its own (src/views.zig), the first the + // session's; or, --shared, every client in the one. + if (!r.shared) for (reps[1..r.clients], 2..) |rp, id| { + const opened = try a.openView(@intCast(id)); + rp.view = opened.view; + for (reps[0..r.clients]) |each| each.confirm(opened.unit); + }; + // Where a wheel over the shell lands in the session's view (the first + // client's); the others' views put it elsewhere, and their wheels hit + // whatever is there. + const sr = a.core.rects[shell]; + const wheel_at: ?[2]u16 = if (sr.h > 2) .{ sr.x + sr.w / 2, sr.y + sr.h / 2 } else null; + + // What the shell's program prints: a full-screen program, a shell's + // session (prompts, titles, directories, a clear now and then), then a + // flood of `yes`, read 4 KB at a time as a pty gives it. + const tui = try screen_state.tuiStream(gpa); + defer gpa.free(tui); + const session = try screen_state.shellStream(gpa); + defer gpa.free(session); + const yes = try screen_state.yesStream(gpa, @intCast(r.flood_mb * (1 << 20) / 3)); + defer gpa.free(yes); + const tui_at: u64 = 400_000; + const session_at: u64 = 1_000_000; + const flood_at: u64 = 2_500_000; + var flood_end: u64 = 0; + var program: struct { bytes: []const u8 = &.{}, at: usize = 0 } = .{}; + // The authority's version of each terminal, as its screen showed then. + var versions: std.AutoHashMapUnmanaged(u64, u64) = .empty; + defer versions.deinit(gpa); + var flood_deltas: [8]u64 = @splat(0); + + var queue: std.PriorityQueue(Msg, void, Msg.order) = .empty; + defer { + while (queue.pop()) |m| if (m.bytes.len > 0) gpa.free(m.bytes); + queue.deinit(gpa); + } + var n: u64 = 0; + const one_way = r.rtt_ms * 1000 / 2; + const jitter = one_way / 10; + var up: [8]Link = @splat(.{}); + var down: [8]Link = @splat(.{}); + var left: [8]usize = @splat(r.steps); + var typing: usize = r.clients; + // Each client's first input, soon; the programs; the first frame. + for (0..r.clients) |c| try push(&queue, &n, .{ .at = random.uintLessThan(u64, 200_000), .n = 0, .kind = .input, .who = @intCast(c + 1), .bytes = &.{} }); + try push(&queue, &n, .{ .at = tui_at, .n = 0, .kind = .output, .who = 0, .bytes = &.{} }); + try push(&queue, &n, .{ .at = session_at, .n = 0, .kind = .output, .who = 2, .bytes = &.{} }); + try push(&queue, &n, .{ .at = flood_at, .n = 0, .kind = .output, .who = 1, .bytes = &.{} }); + try push(&queue, &n, .{ .at = frame_us, .n = 0, .kind = .frame, .who = 0, .bytes = &.{} }); + var frames_left: u32 = 0; + var flood_units: u64 = 0; + + // What each peer is owed of the terminals, onto its link: before the + // authority steps an input, and once a frame. + var owed: std.ArrayListUnmanaged([]u8) = .empty; + defer owed.deinit(gpa); + + while (queue.pop()) |m| { + defer if (m.bytes.len > 0) gpa.free(m.bytes); + switch (m.kind) { + .input => { + const c = m.who - 1; + var buf: [64]u8 = undefined; + const ev = nextEvent(random, &buf, if (c == 0) wheel_at else null, &looks); + const before = reps[c].stats; + const msg = try gpa.dupe(u8, try reps[c].input(ev)); + if (trace) std.debug.print("{d} ms client {d}: {t} {s} -> {s}\n", .{ m.at / 1000, m.who, ev, switch (ev) { + .key => |k| k.text, + .paste => |t| t, + .command => |t| t, + else => "", + }, if (reps[c].stats.speculated > before.speculated) "applied" else if (reps[c].stats.stopped > before.stopped) "STOPPED" else if (reps[c].stats.unspeculated > before.unspeculated) "view" else "waits" }); + try push(&queue, &n, .{ .at = up[c].arrive(random, m.at, one_way, jitter), .n = 0, .kind = .to_authority, .who = m.who, .bytes = msg }); + left[c] -= 1; + if (left[c] > 0) { + // A typist: keys 50-200 ms apart, a pause now and then. + const gap: u64 = if (random.uintLessThan(u8, 12) == 0) random.intRangeAtMost(u64, 300_000, 1_500_000) else random.intRangeAtMost(u64, 50_000, 200_000); + try push(&queue, &n, .{ .at = m.at + gap, .n = 0, .kind = .input, .who = m.who, .bytes = &.{} }); + } else typing -= 1; + }, + .to_authority, .echo, .output => { + const unit = switch (m.kind) { + .echo => echo: { + a.output(.{ .output = .{ .pane = @intCast(m.who), .bytes = m.bytes } }); + break :echo null; + }, + .output => out: { + // The program's next read, 4 KB, then the next a + // fraction of a millisecond on (ghostty's pace). + if (program.at == program.bytes.len) { + program = .{ .bytes = switch (m.who) { + 0 => tui, + 1 => yes, + else => session, + } }; + } + const k = @min(program.bytes.len - program.at, if (m.who == 1) @as(usize, 4096) else 700); + const chunk = program.bytes[program.at..][0..k]; + program.at += k; + if (program.at < program.bytes.len) + try push(&queue, &n, .{ .at = m.at + if (m.who == 1) @as(u64, 450) else 3_000, .n = 0, .kind = .output, .who = m.who, .bytes = &.{} }) + else if (m.who == 1) flood_end = m.at; + if (m.who == 1) flood_units += 1; + a.output(.{ .output = .{ .pane = shell, .bytes = chunk } }); + break :out null; + }, + else => in: { + // The authority's own steps since the last unit, and + // the terminals as they are: what the input's step + // reads, before it is stepped. + if (try a.pending()) |batch| try sendUnit(&queue, &n, batch, m.at, down[0..r.clients], random, one_way, jitter, totals, m.at >= flood_at and flood_end == 0, &screens, a.core, peers[0..r.clients]); + try flushAll(&screens, a.core, peers[0..r.clients], &owed, &queue, &n, m.at, &down, random, one_way, jitter, &versions, totals); + break :in try a.take(m.bytes); + }, + }; + if (unit) |u| try sendUnit(&queue, &n, u, m.at, down[0..r.clients], random, one_way, jitter, totals, false, &screens, a.core, peers[0..r.clients]); + for (host.echo.items) |e| try push(&queue, &n, .{ .at = m.at + 2_000, .n = 0, .kind = .echo, .who = e.pane, .bytes = e.bytes }); + host.echo.clearRetainingCapacity(); + }, + .frame => { + if (try a.pending()) |batch| try sendUnit(&queue, &n, batch, m.at, down[0..r.clients], random, one_way, jitter, totals, m.at >= flood_at and flood_end == 0, &screens, a.core, peers[0..r.clients]); + const t0 = screens.encode_ns; + const before = totals.delta_bytes; + try flushAll(&screens, a.core, peers[0..r.clients], &owed, &queue, &n, m.at, &down, random, one_way, jitter, &versions, totals); + if (m.at >= flood_at and (flood_end == 0 or m.at <= flood_end)) { + totals.flood_delta_bytes += totals.delta_bytes - before; + try totals.frame_ns.append(gpa, screens.encode_ns - t0); + } + // Frames go on while anything moves, and a second after. + const busy = typing > 0 or flood_end == 0 or queue.count() > 0; + frames_left = if (busy) 60 else frames_left -| 1; + if (frames_left > 0) try push(&queue, &n, .{ .at = m.at + frame_us, .n = 0, .kind = .frame, .who = 0, .bytes = &.{} }); + }, + .to_replica, .screen_down => { + const c = m.who - 1; + const rp = reps[c]; + if (m.kind == .to_replica) { + rp.confirm(m.bytes); + rp.settle(); + } else if (rp.screen(m.bytes)) |reply| { + const pane = m.bytes[0]; + if (reply[5] == 1) { + // Applied: the screen is the authority's at that version. + const sc = rp.screens[pane].?; + const vt = &rp.core().panes[pane].?.terminal.?.vt; + totals.screens_checked += 1; + if (versions.get(@as(u64, sc.serial) << 32 | sc.rep.seq)) |want| { + if (want != screenHash(vt)) { + totals.screens_wrong += 1; + std.debug.print("seed {d} rtt {d} ms: replica {d}'s terminal {d} at version {d} is not the authority's\n", .{ r.seed, r.rtt_ms, m.who, pane, sc.rep.seq }); + } + } else totals.screens_wrong += 1; + // Deltas while the flood runs (an RTT/2 behind it). + if (m.at >= flood_at + one_way and (flood_end == 0 or m.at <= flood_end + one_way)) flood_deltas[c] += 1; + } else totals.resets += 1; + try push(&queue, &n, .{ .at = up[c].arrive(random, m.at, one_way, jitter), .n = 0, .kind = .screen_up, .who = m.who, .bytes = try gpa.dupe(u8, reply) }); + } + if (rp.screenRequest()) |req| try push(&queue, &n, .{ .at = up[c].arrive(random, m.at, one_way, jitter), .n = 0, .kind = .screen_up, .who = m.who, .bytes = try gpa.dupe(u8, req) }); + }, + .client_frame => {}, + .screen_up => { + const c = m.who - 1; + if (m.bytes[5] == 2) totals.fetched += std.mem.readInt(u64, m.bytes[14..22], .little) - std.mem.readInt(u64, m.bytes[6..14], .little); + if (try screens.take(a.core, peers[c], m.bytes)) |answer| try push(&queue, &n, .{ .at = down[c].arrive(random, m.at, one_way, jitter), .n = 0, .kind = .screen_down, .who = m.who, .bytes = answer }); + }, + } + } + totals.flood_us += flood_end -| flood_at; + for (flood_deltas[0..r.clients]) |d| totals.flood_deltas += d; + const want = replica.digestOf(a); + for (reps[0..r.clients], 1..) |rp, id| { + try totals.add(&rp.stats); + if (rp.diverged) |why| { + totals.divergences += 1; + std.debug.print("seed {d} rtt {d} ms: replica {d} diverged: {s}\n", .{ r.seed, r.rtt_ms, id, why }); + } else if (!rp.quiet() or rp.digest() != want) { + totals.unequal += 1; + std.debug.print("seed {d} rtt {d} ms: replica {d} at quiescence: {s}\n", .{ r.seed, r.rtt_ms, id, if (rp.quiet()) "another digest" else "inputs still pending" }); + } + // At quiescence, every terminal the authority's, cell for cell. + for (a.core.panes, 0..) |slot, pid| { + const pane = slot orelse continue; + const state = pane.terminal orelse continue; + const mine = (rp.core().panes[pid] orelse { + totals.unequal += 1; + continue; + }).terminal orelse continue; + if (screenHash(&state.vt) != screenHash(&mine.vt)) { + totals.unequal += 1; + std.debug.print("seed {d} rtt {d} ms: replica {d}'s terminal {d} at quiescence is not the authority's\n", .{ r.seed, r.rtt_ms, id, pid }); + } + } + } +} + +fn push(q: *std.PriorityQueue(Msg, void, Msg.order), nn: *u64, m: Msg) !void { + var mm = m; + mm.n = nn.*; + nn.* += 1; + try q.push(gpa, mm); +} + +/// What each peer is owed of the terminals, onto its link (before the +/// authority steps an input, and once a frame); each version as the +/// authority's screen showed it. +fn flushAll(sc: *replica.Screens, core: *pardes.Pardes, ps: []const u16, list: *std.ArrayListUnmanaged([]u8), q: *std.PriorityQueue(Msg, void, Msg.order), nn: *u64, now: u64, dl: []Link, rnd: std.Random, ow: u64, jt: u64, vs: *std.AutoHashMapUnmanaged(u64, u64), tot: *Totals) !void { + for (ps, 0..) |peer, c| { + list.clearRetainingCapacity(); + try sc.flush(core, peer, list); + for (list.items) |bytes| { + tot.deltas += 1; + tot.delta_bytes += bytes.len; + try push(q, nn, .{ .at = dl[c].arrive(rnd, now, ow, jt), .n = 0, .kind = .screen_down, .who = @intCast(c + 1), .bytes = bytes }); + } + } + // Each version as the authority's screen showed it. + for (&sc.tracks, 0..) |*slot, id| if (slot.*) |*tr| { + const key = @as(u64, tr.serial) << 32 | tr.track.version; + if (tr.track.version == 0 or vs.contains(key)) continue; + _ = core.panes[id] orelse continue; + try vs.put(gpa, key, screenHash(&tr.state.vt)); + }; +} + +/// A unit of the authority's to every replica, down its link: first the +/// rows of its terminals its step read that a replica holds as holes. +fn sendUnit(q: *std.PriorityQueue(Msg, void, Msg.order), n: *u64, unit: []const u8, now: u64, down: []Link, random: std.Random, one_way: u64, jitter: u64, totals: *Totals, flooding: bool, screens: *replica.Screens, core: *pardes.Pardes, peers: []const u16) !void { + var rows: std.ArrayListUnmanaged([]u8) = .empty; + defer rows.deinit(gpa); + for (down, peers, 1..) |*l, peer, who| { + rows.clearRetainingCapacity(); + try screens.fill(core, peer, &rows); + for (rows.items) |bytes| { + totals.filled_bytes += bytes.len; + try q.push(gpa, .{ .at = l.arrive(random, now, one_way, jitter), .n = n.*, .kind = .screen_down, .who = @intCast(who), .bytes = bytes }); + n.* += 1; + } + } + screens.filled(core); + // The sizes its steps gave terminals, after their rows (Peers.unit). + pardes.panes.terminal.resizeDeferred(core); + totals.unit_bytes += unit.len * down.len; + if (flooding) totals.flood_unit_bytes += unit.len * down.len; + for (down, 1..) |*l, who| { + try q.push(gpa, .{ .at = l.arrive(random, now, one_way, jitter), .n = n.*, .kind = .to_replica, .who = @intCast(who), .bytes = try gpa.dupe(u8, unit) }); + n.* += 1; + } +} + +// ---- predicted echo (--echo): one client typing a corpus into a shell ---- +// +// The authority's pty is a shell played here (`FakeShell`): a line editor at +// a prompt with pardes's OSC 133 marks and Tab completion, and the programs +// the corpus runs in it: a password prompt (no echo), vim (the alternate +// screen), a program that ignores what is typed, and `cat` (the kernel's +// plain echo, no marks). Each key's echo comes back 2 ms after the authority +// writes it, as a shell's. The client types at a typist's pace and in bursts; +// each of its frames (60 Hz, a phase of its own) is what its screen shows: +// the predicted echo where it is drawn (src/echo.zig), the screen state +// under it. Per key the shell echoed at a prompt or in cat: the time from +// the key to its character on that screen, with prediction and without. Per +// key predicted: whether the authority's screen, right after its echo, +// shows what was predicted where it was. Per key drawn wrong: its frames. + +const ascii: [128][1]u8 = blk: { + var t: [128][1]u8 = undefined; + for (&t, 0..) |*c, i| c.* = .{@intCast(i)}; + break :blk t; +}; + +const Step = struct { gap: u64, key: Key, section: u8 }; + +/// The corpus's parts, each a case of the gates. +const sections = [_][]const u8{ "typing", "a burst", "backspace", "left, right", "tab", "password", "vim", "ignores input", "cat", "typed ahead" }; + +const Corpus = struct { + steps: std.ArrayListUnmanaged(Step) = .empty, + random: std.Random, + extra: u64 = 0, + section: u8 = 0, + + fn add(c: *Corpus, gap: u64, key: Key) !void { + try c.steps.append(gpa, .{ .gap = gap + c.extra, .key = key, .section = c.section }); + c.extra = 0; + } + + fn typing(c: *Corpus, text: []const u8, fast: bool) !void { + for (text) |ch| try c.add(if (fast) c.random.intRangeAtMost(u64, 12_000, 60_000) else c.random.intRangeAtMost(u64, 60_000, 180_000), .{ .cp = ch, .text = &ascii[ch] }); + } + + fn press(c: *Corpus, key: Key) !void { + try c.add(c.random.intRangeAtMost(u64, 80_000, 250_000), key); + } + + fn pause(c: *Corpus) void { + c.extra += c.random.intRangeAtMost(u64, 500_000, 1_200_000); + } + + fn build(c: *Corpus) !void { + const enter: Key = .{ .cp = Key.enter }; + const bs: Key = .{ .cp = Key.backspace }; + c.pause(); + // At a prompt, at a typist's pace, then in a burst. + c.section = 0; + try c.typing("echo hello world", false); + try c.press(enter); + c.pause(); + c.section = 1; + try c.typing("echo the quick brown fox jumps over", true); + try c.press(enter); + c.pause(); + // Backspace. + c.section = 2; + try c.typing("ecoh", false); + try c.press(bs); + try c.press(bs); + try c.typing("ho fixed tpyo", false); + for (0..3) |_| try c.press(bs); + try c.typing("ypo", false); + try c.press(enter); + c.pause(); + // Left and right over what was typed. + c.section = 3; + try c.typing("echo abc", false); + for ([_]u21{ Key.left, Key.left, Key.right, Key.right }) |k| try c.press(.{ .cp = k }); + try c.typing("def", false); + try c.press(enter); + c.pause(); + // Tab completion, typed straight on. + c.section = 4; + try c.typing("ec", false); + try c.press(.{ .cp = Key.tab }); + try c.typing("tab completed", false); + try c.press(enter); + c.pause(); + // A password prompt: nothing typed there may show. + c.section = 5; + try c.typing("sudo ls", false); + try c.press(enter); + c.pause(); + try c.typing("hunter2", false); + try c.press(enter); + c.pause(); + // vim: the alternate screen, never predicted. + c.section = 6; + try c.typing("vim", false); + try c.press(enter); + c.pause(); + try c.typing("ihello from vim", false); + try c.press(.{ .cp = Key.escape, .shift = true }); + try c.typing(":q", false); + try c.press(enter); + c.pause(); + // A program that ignores what is typed. + c.section = 7; + try c.typing("sink", false); + try c.press(enter); + c.pause(); + try c.typing("this goes nowhere", false); + try c.press(.{ .cp = 'c', .ctrl = true }); + c.pause(); + // cat: the kernel's plain echo. + c.section = 8; + try c.typing("cat", false); + try c.press(enter); + c.pause(); + try c.typing("plain echo", false); + try c.press(enter); + try c.typing("a second line", false); + try c.press(enter); + try c.press(.{ .cp = 'd', .ctrl = true }); + c.pause(); + // Typed on before the next prompt. + c.section = 9; + try c.typing("echo one", false); + try c.press(enter); + try c.typing("echo two", false); + try c.press(enter); + c.pause(); + } +}; + +const prompt_bytes = "\x1b]133;A;cl=line;aid=pardes\x07$ \x1b]133;B;aid=pardes\x07"; + +/// The shell on the authority's pty, a key a write (as forwardKey writes). +const FakeShell = struct { + mode: Mode = .prompt, + line: [256]u8 = undefined, + len: usize = 0, + cur: usize = 0, + vim_cmd: [8]u8 = undefined, + vim_n: usize = 0, + out: std.ArrayListUnmanaged(u8) = .empty, + + const Mode = enum { prompt, password, vim, vim_insert, sink, cat }; + + fn put(sh: *FakeShell, bytes: []const u8) void { + sh.out.appendSlice(gpa, bytes) catch {}; + } + + fn print(sh: *FakeShell, comptime fmt: []const u8, args: anytype) void { + sh.out.print(gpa, fmt, args) catch {}; + } + + fn done(sh: *FakeShell, status: u8) void { + sh.print("\x1b]133;D;{d};aid=pardes\x07" ++ prompt_bytes, .{status}); + sh.mode = .prompt; + sh.len = 0; + sh.cur = 0; + } + + fn insert(sh: *FakeShell, c: u8) void { + if (sh.len + 1 >= sh.line.len) return; + sh.put(&.{c}); + if (sh.cur < sh.len) { + sh.put(sh.line[sh.cur..sh.len]); + sh.print("\x1b[{d}D", .{sh.len - sh.cur}); + } + std.mem.copyBackwards(u8, sh.line[sh.cur + 1 .. sh.len + 1], sh.line[sh.cur..sh.len]); + sh.line[sh.cur] = c; + sh.len += 1; + sh.cur += 1; + } + + fn key(sh: *FakeShell, b: []const u8) void { + const printable = b.len == 1 and b[0] >= 0x20 and b[0] < 0x7f; + const eql = std.mem.eql; + switch (sh.mode) { + .prompt => if (printable) sh.insert(b[0]) else if (eql(u8, b, "\x7f")) { + if (sh.cur == 0) return; + if (sh.cur == sh.len) sh.put("\x08\x1b[K") else { + sh.put("\x08"); + sh.put(sh.line[sh.cur..sh.len]); + sh.print(" \x1b[{d}D", .{sh.len - sh.cur + 1}); + } + std.mem.copyForwards(u8, sh.line[sh.cur - 1 .. sh.len - 1], sh.line[sh.cur..sh.len]); + sh.len -= 1; + sh.cur -= 1; + } else if (eql(u8, b, "\x1b[D")) { + if (sh.cur == 0) return; + sh.put(b); + sh.cur -= 1; + } else if (eql(u8, b, "\x1b[C")) { + if (sh.cur == sh.len) return; + sh.put(b); + sh.cur += 1; + } else if (eql(u8, b, "\t")) { + if (sh.cur != sh.len or std.mem.indexOfScalar(u8, sh.line[0..sh.len], ' ') != null) return; + for ([_][]const u8{ "echo", "sudo", "sink", "cat", "vim" }) |cmd| { + if (sh.len == 0 or sh.len >= cmd.len or !std.mem.startsWith(u8, cmd, sh.line[0..sh.len])) continue; + for (cmd[sh.len..]) |ch| sh.insert(ch); + sh.insert(' '); + return; + } + } else if (eql(u8, b, "\r")) { + var cmd_buf: [256]u8 = undefined; + const cmd = cmd_buf[0..sh.len]; + @memcpy(cmd, sh.line[0..sh.len]); + sh.len = 0; + sh.cur = 0; + sh.put("\r\n\x1b]133;C;aid=pardes\x07"); + if (cmd.len == 0) return sh.done(0); + if (std.mem.startsWith(u8, cmd, "echo")) { + sh.print("{s}\r\n", .{std.mem.trimStart(u8, cmd[4..], " ")}); + sh.done(0); + } else if (std.mem.startsWith(u8, cmd, "sudo")) { + sh.put("[sudo] password for you: "); + sh.mode = .password; + } else if (eql(u8, cmd, "vim")) { + sh.put("\x1b[?1049h\x1b[2J\x1b[H~\r\n~\r\n~\x1b[H"); + sh.mode = .vim; + } else if (eql(u8, cmd, "sink")) { + sh.mode = .sink; + } else if (eql(u8, cmd, "cat")) { + sh.mode = .cat; + } else { + sh.print("fake: unknown command: {s}\r\n", .{cmd}); + sh.done(127); + } + } else if (eql(u8, b, "\x03")) { + sh.put("^C\r\n"); + sh.done(130); + }, + .password => if (eql(u8, b, "\r")) { + sh.put("\r\nsudo: 1 incorrect password attempt\r\n"); + sh.done(1); + } else if (eql(u8, b, "\x03")) { + sh.put("^C\r\n"); + sh.done(130); + }, + .vim => if (sh.vim_n > 0) { + if (eql(u8, b, "\r")) { + const quit = eql(u8, sh.vim_cmd[0..sh.vim_n], ":q"); + sh.vim_n = 0; + if (quit) { + sh.put("\x1b[?1049l"); + sh.done(0); + } + } else if (printable and sh.vim_n < sh.vim_cmd.len) { + sh.vim_cmd[sh.vim_n] = b[0]; + sh.vim_n += 1; + } + } else if (eql(u8, b, "i")) { + sh.mode = .vim_insert; + } else if (eql(u8, b, ":")) { + sh.vim_cmd[0] = ':'; + sh.vim_n = 1; + }, + .vim_insert => if (eql(u8, b, "\x1b")) { + sh.mode = .vim; + } else if (printable) sh.put(b), + .sink => if (eql(u8, b, "\x03")) { + sh.put("^C\r\n"); + sh.done(130); + }, + .cat => if (printable) { + sh.put(b); + if (sh.len < sh.line.len) { + sh.line[sh.len] = b[0]; + sh.len += 1; + } + } else if (eql(u8, b, "\x7f")) { + if (sh.len == 0) return; + sh.put("\x08 \x08"); + sh.len -= 1; + } else if (eql(u8, b, "\r")) { + sh.print("\r\n{s}\r\n", .{sh.line[0..sh.len]}); + sh.len = 0; + } else if (eql(u8, b, "\x04") and sh.len == 0) { + sh.done(0); + } else if (eql(u8, b, "\x03")) { + sh.put("^C\r\n"); + sh.done(130); + }, + } + } +}; + +/// The authority's host: each pty write tagged with the client input that +/// made it. +const EchoHost = struct { + writes: std.ArrayListUnmanaged(struct { seq: u32, bytes: []u8 }) = .empty, + current: u32 = 0, + + fn watched(_: ?*anyopaque) bool { + return false; + } + + fn ptyWrite(ctx: ?*anyopaque, _: u8, bytes: []const u8) void { + const h: *EchoHost = @ptrCast(@alignCast(ctx.?)); + h.writes.append(gpa, .{ .seq = h.current, .bytes = gpa.dupe(u8, bytes) catch return }) catch {}; + } + + fn ignore(_: ?*anyopaque, _: []const u8) void {} + + const vtable: pardes.Host.VTable = .{ .watched = watched, .pty_write = ptyWrite, .set_clipboard = ignore }; + + fn host(h: *EchoHost) pardes.Host { + return .{ .ctx = h, .vtable = &vtable }; + } +}; + +const Typed = struct { + seq: u32, + section: u8, + at: u64, + printable: bool, + /// What was predicted: the cell (its row in the active area) and the + /// cursor after it. + pred: ?struct { x: u16, y: u16, cp: u21 } = null, + pcur: ?struct { x: u16, y: u16 } = null, + /// The shell's mode as it took the key; whether it printed anything for + /// it; the authority's version of the screen before that. + mode: ?FakeShell.Mode = null, + echoed: bool = false, + version_before: u32 = 0, + /// The authority's screen right after its echo: its cursor and the + /// cursor's row; whether that is what was predicted. + after: ?struct { x: u16, y: u16, row: [128]u21 } = null, + truth: ?bool = null, + /// Its prediction drawn: first, and in how many frames. + first_visible: ?u64 = null, + visible_frames: u32 = 0, + /// The screen state showing its echo, first. + seen: ?u64 = null, +}; + +const EchoTotals = struct { + keys: u64 = 0, + echo_keys: u64 = 0, + latency: std.ArrayListUnmanaged(u64) = .empty, + predicted: u64 = 0, + unpredicted: u64 = 0, + confirmed: u64 = 0, + mispredicted: u64 = 0, + cursor_missed: u64 = 0, + right: u64 = 0, + wrong: u64 = 0, + wrong_shown: u64 = 0, + wrong_frames: u64 = 0, + wrong_max: u64 = 0, + secret_frames: u64 = 0, + alt_predicted: u64 = 0, + divergences: u64 = 0, + unequal: u64 = 0, + screens_wrong: u64 = 0, + /// The replica's frame drawn while it shows a prediction (its core's + /// render, the tty's and the GUI's), and the overlay's part of it (ns); + /// cells drawn otherwise than predicted. + render_ns: std.ArrayListUnmanaged(u64) = .empty, + paint_ns: std.ArrayListUnmanaged(u64) = .empty, + drawn_frames: u64 = 0, + paint_wrong: u64 = 0, + sec: [sections.len]Section = @splat(.{}), + + const Section = struct { keys: u64 = 0, predicted: u64 = 0, wrong_frames: u64 = 0, latency: std.ArrayListUnmanaged(u64) = .empty }; + + fn deinit(et: *EchoTotals) void { + for (&et.sec) |*x| x.latency.deinit(gpa); + et.latency.deinit(gpa); + et.render_ns.deinit(gpa); + et.paint_ns.deinit(gpa); + } +}; + +fn cellCp(state: *pardes.panes.terminal.State, x: u16, y: u16) u21 { + const ci = state.vt.screens.active.pages.getCell(.{ .active = .{ .x = x, .y = y } }) orelse return 0x10ffff; + const cp = ci.cell.codepoint(); + return if (cp == 0) ' ' else cp; +} + +/// A key's prediction against the authority's screen right after its echo, +/// once both are known (a key typed behind one not predicted is predicted +/// after the authority has it). +fn judgeTruth(k: *Typed) void { + if (k.truth != null) return; + const pc = k.pcur orelse return; + const after = k.after orelse return; + var ok = after.x == pc.x and after.y == pc.y; + if (k.pred) |cell| ok = ok and cell.y == after.y and cell.x < after.row.len and after.row[cell.x] == cell.cp; + k.truth = ok; + if (trace and !ok) std.debug.print("key {d} in {t} predicted cursor {d},{d} cell {any}; the authority's cursor {d},{d}\n", .{ k.seq, k.mode.?, pc.x, pc.y, k.pred, after.x, after.y }); +} + +fn runEcho(seed: u64, rtt_ms: u64, on: bool, et: *EchoTotals) !void { + var prng = std.Random.DefaultPrng.init(seed *% 0x9e3779b97f4a7c15 +% rtt_ms); + const random = prng.random(); + var host: EchoHost = .{}; + defer { + for (host.writes.items) |w| gpa.free(w.bytes); + host.writes.deinit(gpa); + } + var sh: FakeShell = .{}; + defer sh.out.deinit(gpa); + const a = try replica.Authority.init(gpa, .{ .cols = 100, .rows = 30, .boot = .tty_shell }, host.host(), true); + defer a.deinit(); + var screens: replica.Screens = .init(gpa); + defer screens.deinit(a.core); + const peer = try screens.addPeer(); + const peers = [_]u16{peer}; + const rp = try replica.Replica.init(gpa, 1, try a.unit(0, 0), true); + defer rp.deinit(); + rp.echo.on = on; + const shell: u8 = for (a.core.panes, 0..) |slot, id| { + if (slot) |pane| if (pane.isTerminal()) break @intCast(id); + } else return error.NoShell; + var corpus: Corpus = .{ .random = random }; + defer corpus.steps.deinit(gpa); + try corpus.build(); + var typed: std.ArrayListUnmanaged(Typed) = .empty; + defer typed.deinit(gpa); + var t: Totals = .{}; + defer { + t.spec_ns.deinit(gpa); + t.copy_ns.deinit(gpa); + t.rollback_ns.deinit(gpa); + t.apply_ns.deinit(gpa); + t.frame_ns.deinit(gpa); + } + var versions: std.AutoHashMapUnmanaged(u64, u64) = .empty; + defer versions.deinit(gpa); + var queue: std.PriorityQueue(Msg, void, Msg.order) = .empty; + defer { + while (queue.pop()) |m| if (m.bytes.len > 0) gpa.free(m.bytes); + queue.deinit(gpa); + } + var n: u64 = 0; + const one_way = rtt_ms * 1000 / 2; + const jitter = one_way / 10; + var up: [1]Link = @splat(.{}); + var down: [1]Link = @splat(.{}); + var owed: std.ArrayListUnmanaged([]u8) = .empty; + defer owed.deinit(gpa); + + // The shell's first prompt. + sh.put(prompt_bytes); + a.output(.{ .output = .{ .pane = shell, .bytes = sh.out.items } }); + sh.out.clearRetainingCapacity(); + var next_step: usize = 0; + try push(&queue, &n, .{ .at = corpus.steps.items[0].gap, .n = 0, .kind = .input, .who = 1, .bytes = &.{} }); + try push(&queue, &n, .{ .at = frame_us, .n = 0, .kind = .frame, .who = 0, .bytes = &.{} }); + try push(&queue, &n, .{ .at = random.uintLessThan(u64, frame_us), .n = 0, .kind = .client_frame, .who = 1, .bytes = &.{} }); + // Two clocks tick while anything else moves, and a second after. + var periodic: usize = 2; + var left: [2]u32 = .{ 60, 60 }; + + var frame_arena: std.heap.ArenaAllocator = .init(gpa); + defer frame_arena.deinit(); + var made: std.ArrayListUnmanaged(echo.Echo.Predicted) = .empty; + defer made.deinit(gpa); + rp.echo.log = &made; + while (queue.pop()) |m| { + defer if (m.bytes.len > 0) gpa.free(m.bytes); + rp.echo.now = m.at * 1000; + // What it predicted in the last event's calls (a key typed, or the + // keys behind one it did not, at a unit, a delta or a frame). + defer { + const top = rp.echo.tops[shell]; + for (made.items) |pr| { + const k = &typed.items[pr.seq - 1]; + if (trace) { + const sc = rp.core().panes[shell].?.terminal.?.vt.screens.active.cursor; + std.debug.print("{d} ms ({t}): key {d} predicted {any} cursor {d}; acked {d} wait {d} nd {d}; the screen's cursor {d},{d}\n", .{ m.at / 1000, m.kind, pr.seq, pr.cell, pr.cursor.x, rp.echo.acked, rp.echo.wait, rp.echo.nd, sc.x, sc.y }); + } + if (pr.cell) |cell| k.pred = .{ .x = cell.x, .y = @intCast(cell.row - top), .cp = cell.cp }; + k.pcur = .{ .x = pr.cursor.x, .y = @intCast(pr.cursor.row - top) }; + judgeTruth(k); + } + made.clearRetainingCapacity(); + } + switch (m.kind) { + .input => { + const step = corpus.steps.items[next_step]; + next_step += 1; + const msg = try gpa.dupe(u8, try rp.input(.{ .key = step.key })); + const seq = rp.next_seq - 1; + try typed.append(gpa, .{ .seq = seq, .section = step.section, .at = m.at, .printable = step.key.text.len == 1 }); + if (trace) std.debug.print("{d} ms: key {d} typed: '{s}' cp {x}\n", .{ m.at / 1000, seq, step.key.text, step.key.cp }); + try push(&queue, &n, .{ .at = up[0].arrive(random, m.at, one_way, jitter), .n = 0, .kind = .to_authority, .who = 1, .bytes = msg }); + if (next_step < corpus.steps.items.len) try push(&queue, &n, .{ .at = m.at + corpus.steps.items[next_step].gap, .n = 0, .kind = .input, .who = 1, .bytes = &.{} }); + }, + .to_authority => { + if (try a.pending()) |batch| try sendUnit(&queue, &n, batch, m.at, &down, random, one_way, jitter, &t, false, &screens, a.core, &peers); + try flushAll(&screens, a.core, &peers, &owed, &queue, &n, m.at, &down, random, one_way, jitter, &versions, &t); + host.current = (replica.parseInput(m.bytes) orelse return error.BadInput).seq; + const unit = try a.take(m.bytes); + host.current = 0; + try sendUnit(&queue, &n, unit, m.at, &down, random, one_way, jitter, &t, false, &screens, a.core, &peers); + for (host.writes.items) |w| try push(&queue, &n, .{ .at = m.at + 2_000, .n = 0, .kind = .echo, .who = shell, .bytes = w.bytes, .seq = w.seq }); + host.writes.clearRetainingCapacity(); + }, + .echo => { + const ty: ?*Typed = if (m.seq > 0) &typed.items[m.seq - 1] else null; + const state = a.core.panes[shell].?.terminal.?; + if (ty) |k| { + k.mode = sh.mode; + k.version_before = if (screens.tracks[shell]) |*tr| tr.track.version else 0; + } + sh.key(m.bytes); + if (sh.out.items.len > 0) { + a.output(.{ .output = .{ .pane = shell, .bytes = sh.out.items } }); + sh.out.clearRetainingCapacity(); + if (ty) |k| k.echoed = true; + } + if (ty) |k| { + const cur = state.vt.screens.active.cursor; + k.after = .{ .x = @intCast(cur.x), .y = @intCast(cur.y), .row = undefined }; + for (&k.after.?.row, 0..) |*cp, x| cp.* = cellCp(state, @intCast(x), @intCast(cur.y)); + judgeTruth(k); + } + }, + .frame => { + if (try a.pending()) |batch| try sendUnit(&queue, &n, batch, m.at, &down, random, one_way, jitter, &t, false, &screens, a.core, &peers); + try flushAll(&screens, a.core, &peers, &owed, &queue, &n, m.at, &down, random, one_way, jitter, &versions, &t); + const busy = next_step < corpus.steps.items.len or queue.count() + 1 > periodic; + left[0] = if (busy) 60 else left[0] -| 1; + if (left[0] > 0) try push(&queue, &n, .{ .at = m.at + frame_us, .n = 0, .kind = .frame, .who = 0, .bytes = &.{} }) else periodic -= 1; + }, + .client_frame => { + _ = rp.echoFrame(); + const core = rp.core(); + const drawn = rp.echo.visible(core) and core.panes[shell].?.body.mode == .tty; + if (drawn and rp.echo.n > 0) { + // Drawn as the client draws it: each cell where it was + // predicted. + _ = frame_arena.reset(.retain_capacity); + core.needs_frame = true; + const t0 = pardes.FrameStats.nowNs(); + const surf = try core.render(frame_arena.allocator()); + try et.render_ns.append(gpa, pardes.FrameStats.nowNs() - t0); + const body = surf.region(.body, shell).?.rect; + const top = rp.echo.tops[shell]; + for (rp.echo.cells[0..rp.echo.n]) |cell| { + if (cell.row < top or surf.at(body.x + cell.x, body.y + @as(u16, @intCast(cell.row - top))).text[0] != cell.cp) et.paint_wrong += 1; + } + const t1 = pardes.FrameStats.nowNs(); + rp.echo.paint(core, surf, core.panes[shell].?, shell, body.x, body.w, body.y, body.h, true); + try et.paint_ns.append(gpa, pardes.FrameStats.nowNs() - t1); + et.drawn_frames += 1; + } + if (drawn) for (rp.echo.cells[0..rp.echo.n]) |cell| { + const k = &typed.items[cell.seq - 1]; + k.visible_frames += 1; + if (k.first_visible == null) { + k.first_visible = m.at; + if (trace) { + const st = core.panes[shell].?.terminal.?; + std.debug.print("{d} ms: key {d} ('{u}') drawn: trusted {} doubt {} phase {t} sem {t}\n", .{ m.at / 1000, cell.seq, cell.cp, rp.echo.trusted, rp.echo.doubt, st.stream.handler.phase, st.vt.screens.active.cursor.semantic_content }); + } + } + }; + const applied = if (rp.screens[shell]) |sc| sc.rep.seq else 0; + for (typed.items) |*k| if (k.echoed and k.seen == null and applied > k.version_before) { + k.seen = m.at; + }; + const busy = next_step < corpus.steps.items.len or queue.count() + 1 > periodic; + left[1] = if (busy) 60 else left[1] -| 1; + if (left[1] > 0) try push(&queue, &n, .{ .at = m.at + frame_us, .n = 0, .kind = .client_frame, .who = 1, .bytes = &.{} }) else periodic -= 1; + }, + .to_replica => { + rp.confirm(m.bytes); + rp.settle(); + }, + .screen_down => { + if (rp.screen(m.bytes)) |reply| { + const pane = m.bytes[0]; + if (reply[5] == 1) { + const sc = rp.screens[pane].?; + if (versions.get(@as(u64, sc.serial) << 32 | sc.rep.seq)) |want| { + if (want != screenHash(&rp.core().panes[pane].?.terminal.?.vt)) t.screens_wrong += 1; + } else t.screens_wrong += 1; + } + try push(&queue, &n, .{ .at = up[0].arrive(random, m.at, one_way, jitter), .n = 0, .kind = .screen_up, .who = 1, .bytes = try gpa.dupe(u8, reply) }); + } + if (rp.screenRequest()) |req| try push(&queue, &n, .{ .at = up[0].arrive(random, m.at, one_way, jitter), .n = 0, .kind = .screen_up, .who = 1, .bytes = try gpa.dupe(u8, req) }); + }, + .screen_up => { + if (try screens.take(a.core, peer, m.bytes)) |answer| try push(&queue, &n, .{ .at = down[0].arrive(random, m.at, one_way, jitter), .n = 0, .kind = .screen_down, .who = 1, .bytes = answer }); + }, + .output => {}, + } + } + const s = rp.echo.stats; + et.predicted += s.predicted; + et.unpredicted += s.unpredicted; + et.confirmed += s.confirmed; + et.mispredicted += s.mispredicted; + et.cursor_missed += s.cursor_missed; + et.screens_wrong += t.screens_wrong; + for (typed.items) |k| { + et.keys += 1; + const sec = &et.sec[k.section]; + sec.keys += 1; + if (k.pcur != null) sec.predicted += 1; + if (k.visible_frames > 0 and k.truth != true) sec.wrong_frames += k.visible_frames; + if (k.truth) |ok| { + if (ok) et.right += 1 else et.wrong += 1; + } + const md = k.mode orelse .prompt; + if (md == .password or md == .sink) et.secret_frames += k.visible_frames; + if ((md == .vim or md == .vim_insert) and k.pcur != null) et.alt_predicted += 1; + if (k.visible_frames > 0 and k.truth != true) { + if (trace) std.debug.print("seed {d}: key {d} at {d} ms in {t} drawn {d} frames, truth {?}, predicted {any} cursor {any}, the authority after it {any}\n", .{ seed, k.seq, k.at / 1000, md, k.visible_frames, k.truth, k.pred, k.pcur, if (k.after) |af| .{ af.x, af.y } else null }); + et.wrong_shown += 1; + et.wrong_frames += k.visible_frames; + et.wrong_max = @max(et.wrong_max, k.visible_frames); + } + if (k.printable and k.echoed and (md == .prompt or md == .cat)) { + const seen = k.seen orelse std.math.maxInt(u64); + const drawn = if (k.first_visible != null and k.truth == true) @min(k.first_visible.?, seen) else seen; + if (drawn == std.math.maxInt(u64)) { + std.debug.print("seed {d} rtt {d} ms: a key's echo was never drawn\n", .{ seed, rtt_ms }); + et.unequal += 1; + continue; + } + et.echo_keys += 1; + try et.latency.append(gpa, drawn - k.at); + try sec.latency.append(gpa, drawn - k.at); + if (trace and drawn - k.at > 2 * frame_us) std.debug.print("key {d} at {d} ms in {t}: drawn after {d} ms, {s}\n", .{ k.seq, k.at / 1000, md, (drawn - k.at) / 1000, if (k.pcur == null) "not predicted" else if (k.first_visible == null) "predicted, not drawn" else if (k.truth != true) "predicted wrong" else "predicted, drawn late" }); + } + } + if (rp.diverged) |why| { + et.divergences += 1; + std.debug.print("seed {d} rtt {d} ms: the replica diverged: {s}\n", .{ seed, rtt_ms, why }); + } else if (!rp.quiet() or rp.digest() != replica.digestOf(a)) { + et.unequal += 1; + std.debug.print("seed {d} rtt {d} ms: the replica at quiescence: {s}\n", .{ seed, rtt_ms, if (rp.quiet()) "another digest" else "inputs still pending" }); + } + if (screenHash(&a.core.panes[shell].?.terminal.?.vt) != screenHash(&rp.core().panes[shell].?.terminal.?.vt)) { + et.unequal += 1; + std.debug.print("seed {d} rtt {d} ms: the shell at quiescence is not the authority's\n", .{ seed, rtt_ms }); + } +} + +fn echoMain(seeds: u64, first: u64, rtts: []const u64) !bool { + var failed = false; + var draw_lines: std.ArrayList(u8) = .empty; + defer draw_lines.deinit(gpa); + std.debug.print("predicted echo: one client typing the corpus into a shell, {d} seeds a cell; from a key to its character on the client's screen (ms, frames at 60 Hz)\n", .{seeds}); + std.debug.print(" rtt echo keys echoed p50 p90 max predicted unpredicted right/wrong confirmed mispredicted cursor-missed drawn-wrong keys/frames/max secret-frames vim-predicted diverged unequal screens-wrong\n", .{}); + var by_section: std.ArrayList(u8) = .empty; + defer by_section.deinit(gpa); + var off: [sections.len][2]u64 = undefined; + for (rtts) |rtt| for ([_]bool{ false, true }) |on| { + var et: EchoTotals = .{}; + defer et.deinit(); + for (first..first + seeds) |seed| try runEcho(seed, rtt, on, &et); + const ms = struct { + fn f(us: u64) f64 { + return @as(f64, @floatFromInt(us)) / 1000; + } + }.f; + std.debug.print("{d:>4} {s:>4} {d:>4} {d:>6} {d:>5.1} {d:>5.1} {d:>5.1} {d:>9} {d:>11} {d:>5}/{d:<5} {d:>9} {d:>12} {d:>13} {d:>5}/{d:>5}/{d:<5} {d:>13} {d:>13} {d:>8} {d:>7} {d:>13}\n", .{ + rtt, if (on) "on" else "off", et.keys, et.echo_keys, + ms(pct(et.latency.items, 50)), ms(pct(et.latency.items, 90)), ms(pct(et.latency.items, 100)), et.predicted, + et.unpredicted, et.right, et.wrong, et.confirmed, + et.mispredicted, et.cursor_missed, et.wrong_shown, et.wrong_frames, + et.wrong_max, et.secret_frames, et.alt_predicted, et.divergences, + et.unequal, et.screens_wrong, + }); + for (&et.sec, &off, sections) |*x, *o, name| { + const p50 = pct(x.latency.items, 50) / 1000; + const p90 = pct(x.latency.items, 90) / 1000; + if (!on) { + o.* = .{ p50, p90 }; + continue; + } + try by_section.print(gpa, "{d:>4} {s:<14} {d:>5} {d:>9} {d:>7} {d:>4} -> {d:<4} {d:>4} -> {d:<4} {d:>12}\n", .{ rtt, name, x.keys, x.predicted, x.latency.items.len, o[0], p50, o[1], p90, x.wrong_frames }); + } + if (on) try draw_lines.print(gpa, "{d:>4} {d:>12} {d:>6}/{d:<6} {d:>5}/{d:<5} {d:>10}\n", .{ rtt, et.drawn_frames, pct(et.render_ns.items, 50) / 1000, pct(et.render_ns.items, 99) / 1000, pct(et.paint_ns.items, 50), pct(et.paint_ns.items, 99), et.paint_wrong }); + if (et.divergences > 0 or et.unequal > 0 or et.screens_wrong > 0 or et.secret_frames > 0 or et.alt_predicted > 0 or et.paint_wrong > 0) failed = true; + }; + std.debug.print("by part of the corpus (all seeds): keys typed, predicted, echoed (the latency sample); key to its character on the screen, ms, without -> with; frames a wrong prediction was drawn\n rtt part keys predicted echoed p50 off/on p90 off/on drawn-wrong\n{s}", .{by_section.items}); + std.debug.print("the client's frames drawn with a prediction shown: its render (us) and the overlay's part (ns); cells drawn otherwise than predicted\n rtt frames-drawn render p50/p99 paint p50/p99 drawn-wrong\n{s}", .{draw_lines.items}); + return !failed; +} + + +fn pct(samples: []u64, p: usize) u64 { + if (samples.len == 0) return 0; + std.mem.sort(u64, samples, {}, std.sort.asc(u64)); + return samples[@min(samples.len - 1, samples.len * p / 100)]; +} + +pub fn main(init: std.process.Init) !void { + const args = try init.minimal.args.toSlice(init.arena.allocator()); + var seeds: u64 = 4; + var first: u64 = 0; + var steps: usize = 120; + var clients: u16 = 3; + var rtts: []const u64 = &.{ 30, 80, 207 }; + var shared = false; + var echo_mode = false; + var flood_mb: u64 = 8; + var rtt_buf: [8]u64 = undefined; + var i: usize = 1; + while (i < args.len) : (i += 1) { + const name = args[i]; + if (std.mem.eql(u8, name, "--shared")) { + shared = true; + continue; + } + if (std.mem.eql(u8, name, "--echo")) { + echo_mode = true; + continue; + } + if (i + 1 >= args.len) return error.MissingValue; + i += 1; + const value = args[i]; + if (std.mem.eql(u8, name, "--seeds")) seeds = try std.fmt.parseInt(u64, value, 10) else if (std.mem.eql(u8, name, "--first")) first = try std.fmt.parseInt(u64, value, 10) else if (std.mem.eql(u8, name, "--steps")) steps = try std.fmt.parseInt(usize, value, 10) else if (std.mem.eql(u8, name, "--clients")) clients = @min(8, try std.fmt.parseInt(u16, value, 10)) else if (std.mem.eql(u8, name, "--flood")) flood_mb = try std.fmt.parseInt(u64, value, 10) else if (std.mem.eql(u8, name, "--rtt")) { + var it = std.mem.tokenizeScalar(u8, value, ','); + var k: usize = 0; + while (it.next()) |v| : (k += 1) rtt_buf[k] = try std.fmt.parseInt(u64, v, 10); + rtts = rtt_buf[0..k]; + } else return error.UnknownOption; + } + trace = libc.getenv("PARDES_REPLICA_TRACE") != null; + if (libc.getenv("PARDES_REPLICA_FILL")) |b| pardes.rollback.Epoch.fill = std.fmt.parseInt(u8, std.mem.span(b), 0) catch 0xff; + var dir_buf: [256]u8 = undefined; + const tmp = std.mem.span(libc.getenv("TMPDIR") orelse "/tmp"); + const dir = try std.fmt.bufPrint(&dir_buf, "{s}/replicas-{d}", .{ tmp, @as(u32, @intCast(libc.getpid())) }); + try std.Io.Dir.createDirAbsolute(init.io, dir, .default_dir); + defer std.Io.Dir.cwd().deleteTree(init.io, dir) catch {}; + if (echo_mode) { + if (!try echoMain(seeds, first, rtts)) std.process.exit(1); + return; + } + var failed = false; + std.debug.print("{d} seeds x {d} inputs from each of {d} clients, {s}, a TUI then {d} MB of yes in the shell; costs in us (frame {d})\n", .{ seeds, steps, clients, if (shared) "sharing one view" else "a view each", flood_mb, frame_us }); + std.debug.print(" rtt inputs at-once stopped view units fast rollback(mis+foreign) rate rebase spec p50/p99 copy p50/p99 (KB) rollback p50/p99 diverged unequal\n", .{}); + var term_lines: std.ArrayList(u8) = .empty; + defer term_lines.deinit(gpa); + for (rtts) |rtt| { + var t: Totals = .{}; + for (first..first + seeds) |seed| try run(.{ .seed = seed, .rtt_ms = rtt, .steps = steps, .clients = clients, .shared = shared, .flood_mb = flood_mb }, dir, &t); + const rollbacks = t.mispredicted + t.foreign; + std.debug.print("{d:>4} {d:>6} {d:>7} {d:>7} {d:>4} {d:>6} {d:>6} {d:>6} ({d}+{d}) {d:>5.1}% {d:>6} {d:>5}/{d:<6} {d:>5}/{d:<5} ({d}) {d:>7}/{d:<7} {d:>8} {d:>7}\n", .{ + rtt, t.inputs, t.speculated, + t.stopped, t.unspeculated, t.units, + t.fast, rollbacks, t.mispredicted, + t.foreign, @as(f64, @floatFromInt(rollbacks)) * 100 / @as(f64, @floatFromInt(@max(t.inputs, 1))), t.rebases, + pct(t.spec_ns.items, 50) / 1000, pct(t.spec_ns.items, 99) / 1000, pct(t.copy_ns.items, 50) / 1000, + pct(t.copy_ns.items, 99) / 1000, t.copy_bytes / @max(t.copies, 1) / 1024, pct(t.rollback_ns.items, 50) / 1000, + pct(t.rollback_ns.items, 99) / 1000, t.divergences, t.unequal, + }); + const flood_s = @as(f64, @floatFromInt(@max(1, t.flood_us))) / 1e6; + + try term_lines.print(gpa, "{d:>4} {d:>7.1} {d:>9.0} {d:>9.0} {d:>5}/{d:<5} {d:>5}/{d:<5} {d:>7} {d:>9} {d:>6} {d:>6}/{d:<6}\n", .{ + rtt, + @as(f64, @floatFromInt(t.flood_deltas)) / flood_s / @as(f64, @floatFromInt(clients)), + @as(f64, @floatFromInt(t.flood_delta_bytes)) / flood_s / @as(f64, @floatFromInt(clients)), + @as(f64, @floatFromInt(t.flood_unit_bytes)) / flood_s / @as(f64, @floatFromInt(clients)), + pct(t.frame_ns.items, 50) / 1000, + pct(t.frame_ns.items, 99) / 1000, + pct(t.apply_ns.items, 50) / 1000, + pct(t.apply_ns.items, 99) / 1000, + t.fetched, + t.filled_bytes / 1024, + t.resets, + t.screens_checked, + t.screens_wrong, + }); + if (t.divergences > 0 or t.unequal > 0 or t.screens_wrong > 0) failed = true; + } + std.debug.print("terminals: deltas a replica applied a second under the flood; a peer's bytes a second under the flood (deltas, units); encode a frame of the flood for every peer, apply a delta (us); rows fetched, KB of rows sent before the step that read them; resets; deltas checked/wrong\n", .{}); + std.debug.print(" rtt deltas/s delta B/s unit B/s encode p50/p99 apply p50/p99 fetched filled KB resets checked/wrong\n{s}", .{term_lines.items}); + if (failed) std.process.exit(1); +} diff --git a/test/rollback_bench.zig b/test/rollback_bench.zig new file mode 100644 index 00000000..0f0c77a4 --- /dev/null +++ b/test/rollback_bench.zig @@ -0,0 +1,226 @@ +//! What a replica's rollback copy costs (docs/next-daemon.md, open point +//! 3, src/rollback.zig), and what a fork of the authority stalls it for +//! (open point 5). A typical session and a big one are built in process, on +//! a rollback epoch's allocator as a replica's core is; for each: +//! - the copy taken and let go: its time, the bytes it saves, the bytes +//! it pins (a file's text, its undo snapshots); +//! - a key that edits a file (`x`) applied on top, as a speculation, and +//! the copy put back: the apply's time and the restore's. +//! Then a fork of the big one, child gone at once, and the authority's +//! copy-on-write after it. +//! +//! zig build rollback-bench -Dplatform=tty -Doptimize=ReleaseFast +const std = @import("std"); +const libc = std.c; +const pardes = @import("pardes"); +const walk = pardes.walk; + +const gpa = std.heap.c_allocator; + +fn nowNs() u64 { + var ts: libc.timespec = undefined; + _ = libc.clock_gettime(.MONOTONIC, &ts); + return @as(u64, @intCast(ts.sec)) *| 1_000_000_000 +| @as(u64, @intCast(ts.nsec)); +} + +const File = struct { lines: usize, copies: usize = 1 }; + +const Session = struct { + name: []const u8, + files: []const File, + /// The shell's scrollback, fed through its terminal. + scrollback_kb: usize, + /// Insert sessions (`i x Esc`), each an undo step, spread over the files. + edits: usize, +}; + +// 60-column lines: 1k lines is 60 KB, 50k is 3 MB, 300k is 18 MB. +const sessions = [_]Session{ + .{ .name = "typical", .files = &.{ .{ .lines = 1_000, .copies = 3 }, .{ .lines = 50_000 } }, .scrollback_kb = 1024, .edits = 40 }, + .{ .name = "big", .files = &.{ .{ .lines = 1_000, .copies = 8 }, .{ .lines = 50_000, .copies = 4 }, .{ .lines = 300_000 } }, .scrollback_kb = 8 * 1024, .edits = 200 }, +}; + +fn pump(core: *pardes.Pardes) void { + while (core.nextEffect()) |_| {} +} + +fn key(core: *pardes.Pardes, cp: u21, text: []const u8) void { + core.update(.{ .key = .{ .cp = cp, .text = text } }); + pump(core); +} + +fn writeFile(path: []const u8, lines: usize) !void { + var out: std.Io.Writer.Allocating = .init(gpa); + defer out.deinit(); + for (0..lines) |n| try out.writer.print("const value_{d:0>7}: u32 = {d:0>10}; // a line of sixty\n", .{ n, n *% 2654435761 }); + var pathbuf: [4096]u8 = undefined; + const fd = libc.open(try std.fmt.bufPrintSentinel(&pathbuf, "{s}", .{path}, 0), .{ .ACCMODE = .WRONLY, .CREAT = true, .TRUNC = true }, @as(libc.mode_t, 0o644)); + if (fd < 0) return error.OpenFailed; + defer _ = libc.close(fd); + const text = out.written(); + var off: usize = 0; + while (off < text.len) { + const n = libc.write(fd, text.ptr + off, text.len - off); + if (n <= 0) return error.WriteFailed; + off += @intCast(n); + } +} + +fn build(s: Session, dir: []const u8, a: std.mem.Allocator) !*pardes.Pardes { + const core = try pardes.Pardes.init(a, .{ .tty_only = true, .image_allocator = a, .pdf_allocator = a, .tree_sitter_allocator = a, .frame_allocator = a }); + core.update(.{ .resize = .{ .cols = 200, .rows = 60 } }); + pump(core); + // The shell: pane 0 of a tty boot, its output fed as a pty's. + var chunk: std.Io.Writer.Allocating = .init(gpa); + defer chunk.deinit(); + var n: usize = 0; + while (chunk.written().len < s.scrollback_kb * 1024) : (n += 1) + try chunk.writer.print("\x1b[90m[{d:0>6}]\x1b[0m compiling module_{d} ... \x1b[32mok\x1b[0m\r\n", .{ n, n }); + var off: usize = 0; + while (off < chunk.written().len) { + const end = @min(off + 8192, chunk.written().len); + core.update(.{ .output = .{ .pane = 0, .bytes = chunk.written()[off..end] } }); + pump(core); + off = end; + } + var opened: [pardes.MAX_PANES]usize = undefined; + var nopened: usize = 0; + for (s.files, 0..) |f, fi| for (0..f.copies) |c| { + var buf: [512]u8 = undefined; + const path = try std.fmt.bufPrint(&buf, "{s}/f{d}-{d}.zig", .{ dir, fi, c }); + try writeFile(path, f.lines); + pardes.look.lookAt(core, 0, path); + pump(core); + for (core.panes, 0..) |slot, id| if (slot) |pane| if (pane.file) |file| if (std.mem.eql(u8, file.path, path)) { + opened[nopened] = id; + nopened += 1; + }; + }; + if (nopened == 0) return error.NothingOpened; + for (0..s.edits) |e| { + core.active = opened[e % nopened]; + key(core, 'i', "i"); + key(core, 'x', "x"); + key(core, pardes.Key.escape, ""); + } + var arena: std.heap.ArenaAllocator = .init(gpa); + defer arena.deinit(); + _ = try core.render(arena.allocator()); + return core; +} + +fn median(samples: []u64) u64 { + std.mem.sort(u64, samples, {}, std.sort.asc(u64)); + return samples[samples.len / 2]; +} + +fn rssKb() usize { + var ru: libc.rusage = undefined; + _ = libc.getrusage(libc.rusage.SELF, &ru); + return @intCast(ru.maxrss); +} + +const reps = 15; + +pub fn main(init: std.process.Init) !void { + var dir_buf: [256]u8 = undefined; + const tmp = std.mem.span(libc.getenv("TMPDIR") orelse "/tmp"); + const dir = try std.fmt.bufPrint(&dir_buf, "{s}/rollback-bench-{d}", .{ tmp, @as(u32, @intCast(libc.getpid())) }); + try std.Io.Dir.createDirAbsolute(init.io, dir, .default_dir); + defer std.Io.Dir.cwd().deleteTree(init.io, dir) catch {}; + std.debug.print("session panes copy KB pinned MB take ms release ms edit ms restore ms\n", .{}); + var big: ?*pardes.Pardes = null; + var epochs: [sessions.len]*pardes.rollback.Epoch = undefined; + for (sessions, &epochs) |s, *e| { + e.* = try gpa.create(pardes.rollback.Epoch); + e.*.* = .{}; + const core = try build(s, dir, e.*.allocator()); + var c: pardes.rollback.Copy = .{}; + defer c.deinit(); + var take: [reps]u64 = undefined; + var release: [reps]u64 = undefined; + for (&take, &release) |*x, *y| { + const t0 = nowNs(); + try c.take(e.*, core); + const t1 = nowNs(); + c.release(); + x.* = t1 - t0; + y.* = nowNs() - t1; + } + var edit: [reps]u64 = undefined; + var restore: [reps]u64 = undefined; + for (&edit, &restore) |*x, *y| { + try c.take(e.*, core); + const t0 = nowNs(); + key(core, 'x', "x"); + const t1 = nowNs(); + c.restore(); + x.* = t1 - t0; + y.* = nowNs() - t1; + } + // A join (docs/next-daemon.md, Phase 5 part B notes): the session + // dumped at a step boundary, which is what the authority pays, and + // the joiner's load of it. + var dump: [5]u64 = undefined; + var load: [5]u64 = undefined; + var dump_len: usize = 0; + for (&dump, &load) |*x, *y| { + const t0 = nowNs(); + const bytes = try pardes.dump.dumpBytes(core); + const t1 = nowNs(); + dump_len = bytes.len; + const loaded = try pardes.dump.load(gpa, .{ .tty_only = true, .image_allocator = gpa, .pdf_allocator = gpa, .tree_sitter_allocator = gpa, .frame_allocator = gpa }, bytes, null); + y.* = nowNs() - t1; + x.* = t1 - t0; + loaded.deinit(); + core.gpa.free(bytes); + } + std.debug.print("{s:<8} join: dump {d:.1} MB in {d:.2} ms (the session's step), load {d:.2} ms (the joiner's)\n", .{ s.name, @as(f64, @floatFromInt(dump_len)) / 1e6, @as(f64, @floatFromInt(median(&dump))) * 1e-6, @as(f64, @floatFromInt(median(&load))) * 1e-6 }); + var panes: usize = 0; + for (core.panes) |slot| panes += @intFromBool(slot != null); + const ms = 1e-6; + std.debug.print("{s:<8} {d:>5} {d:>7.1} {d:>9.1} {d:>7.3} {d:>10.3} {d:>7.3} {d:>10.3}\n", .{ + s.name, panes, + @as(f64, @floatFromInt(c.savedBytes())) / 1e3, @as(f64, @floatFromInt(c.pinnedBytes())) / 1e6, + @as(f64, @floatFromInt(median(&take))) * ms, @as(f64, @floatFromInt(median(&release))) * ms, + @as(f64, @floatFromInt(median(&edit))) * ms, @as(f64, @floatFromInt(median(&restore))) * ms, + }); + if (big) |b| b.deinit(); + big = core; + } + // The join writer as a fork child: what fork() holds the authority for, + // and its copy-on-write after, the child alive. The child waits on a + // pipe and exits. + const core = big.?; + defer core.deinit(); + var t: [reps]u64 = undefined; + var cow: [reps]u64 = undefined; + const touch = try gpa.alloc(u8, 64 << 20); + defer gpa.free(touch); + @memset(touch, 1); + for (&t, &cow) |*x, *c| { + var pipe: [2]c_int = undefined; + if (libc.pipe(&pipe) != 0) return error.Pipe; + const t0 = nowNs(); + const pid = libc.fork(); + if (pid == 0) { + var one: [1]u8 = undefined; + _ = libc.read(pipe[0], &one, 1); + libc._exit(0); + } + x.* = nowNs() - t0; + // 64 MB the authority writes while the child holds its pages. + const t1 = nowNs(); + var i: usize = 0; + while (i < touch.len) : (i += 4096) touch[i] +%= 1; + c.* = nowNs() - t1; + _ = libc.close(pipe[0]); + _ = libc.write(pipe[1], "x", 1); + _ = libc.close(pipe[1]); + var status: c_int = 0; + _ = libc.waitpid(pid, &status, 0); + } + std.debug.print("fork of the big session (max RSS {d} MB): {d:.3} ms; then 64 MB written under the child: {d:.3} ms\n", .{ + rssKb() / 1024, @as(f64, @floatFromInt(median(&t))) / 1e6, @as(f64, @floatFromInt(median(&cow))) / 1e6, + }); +} diff --git a/test/snapshot.zig b/test/snapshot.zig index 2d8a8664..1bd4dabf 100644 --- a/test/snapshot.zig +++ b/test/snapshot.zig @@ -29,7 +29,11 @@ // lines <name> <n> [tail] create file with n numbered lines, tail on each // dirmk <name> create a subdirectory // run <shell...> run a command in the script's cwd (NOT in a pane) -// start <rows> <cols> [arg] fork the app in a pty (optional extra CLI arg) +// start <rows> <cols> [views=N] [arg] fork the app in a pty (optional +// extra CLI arg); views=N gives it N-1 more +// terminals, each a view of the session +// view <k> type into and capture terminal k (1 is the +// app's own; a resize there SIGWINCHes the app) // wait <ms> <needle...> pump until needle appears on the grid (fails hard) // settle <ms> pump for a fixed duration // stable <quiet_ms> <timeout_ms> pump until the grid stops changing @@ -74,6 +78,7 @@ const Client = @import("9p_io").Client; pub const std_options: std.Options = .{ .log_level = .err }; extern "c" fn setenv(name: [*:0]const u8, value: [*:0]const u8, overwrite: c_int) c_int; +extern "c" fn unsetenv(name: [*:0]const u8) c_int; extern "c" fn execvp(file: [*:0]const u8, argv: [*:null]const ?[*:0]const u8) c_int; extern "c" fn system(cmd: [*:0]const u8) c_int; extern "c" fn mkdtemp(template: [*:0]u8) ?[*:0]u8; @@ -86,6 +91,17 @@ var careful = false; /// (--lspmock=); null leaves the protocol client fully disabled for the run var lspmock_bin: ?[:0]const u8 = null; var record_dir: ?[]const u8 = null; +/// --tape=DIR: each session records its tape there (PARDES_RECORD, src/tape.zig) +/// and the script fails unless every tape replays without diverging. +var tape_dir: ?[]const u8 = null; +/// --replay-with=EXE (with --tape): each tape replayed by that build of +/// pardes too, a window's build replaying a terminal's sessions (a replica +/// of another build reads them so: docs/next-daemon.md). +var replay_with: ?[:0]const u8 = null; +/// --dump (with --tape): each replay also loads the session's dump at its +/// end, which must be the same session and dump to the same bytes +/// (PARDES_REPLAY_DUMP, src/tape.zig). +var dump_check = false; const gpa = std.heap.page_allocator; pub fn main(init: std.process.Init) !void { @@ -123,6 +139,12 @@ pub fn main(init: std.process.Init) !void { lspmock = try absPath(arena, orig_cwd, a["--lspmock=".len..]); } else if (std.mem.startsWith(u8, a, "--record=")) { record_dir = try absPath(arena, orig_cwd, a["--record=".len..]); + } else if (std.mem.startsWith(u8, a, "--tape=")) { + tape_dir = try absPath(arena, orig_cwd, a["--tape=".len..]); + } else if (std.mem.eql(u8, a, "--dump")) { + dump_check = true; + } else if (std.mem.startsWith(u8, a, "--replay-with=")) { + replay_with = try arena.dupeSentinel(u8, try absPath(arena, orig_cwd, a["--replay-with=".len..]), 0); } else if (std.mem.startsWith(u8, a, "--jobs=")) { jobs = @max(1, try std.fmt.parseInt(usize, a["--jobs=".len..], 10)); } else if (exe == null) { @@ -148,6 +170,9 @@ pub fn main(init: std.process.Init) !void { std.mem.eql(u8, a, "--trace-stable") or std.mem.startsWith(u8, a, "--lspmock=") or std.mem.startsWith(u8, a, "--record=") or + std.mem.startsWith(u8, a, "--tape=") or + std.mem.eql(u8, a, "--dump") or + std.mem.startsWith(u8, a, "--replay-with=") or std.mem.startsWith(u8, a, "--jobs=")) continue; if (!saw_exe) { saw_exe = true; @@ -179,6 +204,7 @@ pub fn main(init: std.process.Init) !void { if (update) fatal("--record and --update are separate modes", .{}); try mkdir(arena, dir, true); } + if (tape_dir) |dir| try mkdir(arena, dir, true); try mkdir(arena, SNAP_BASE, true); // one run at a time (this is the flock that used to be typed by hand) @@ -463,9 +489,15 @@ fn runOne(arena: std.mem.Allocator, exe_z: [:0]const u8, script_path: []const u8 const started = eh.nowMs(); const out = runScript(arena, exe_z, script_path, stem) catch |e| { std.debug.print("FAIL {s}: script error {s} ({d}ms)\n", .{ stem, @errorName(e), eh.nowMs() - started }); + // What the session did before the script gave up replays all the same. + if (tape_dir != null) if (replayTapes(arena, exe_z, stem)) |why| std.debug.print("DIVERGED {s}: {s}\n", .{ stem, why }); return 1; }; const took = eh.nowMs() - started; + if (tape_dir != null) if (replayTapes(arena, exe_z, stem)) |why| { + std.debug.print("DIVERGED {s}: {s}\n", .{ stem, why }); + return 1; + }; if (record_dir) |dir| { const path = std.fmt.allocPrintSentinel(arena, "{s}/{s}.snapshot", .{ dir, stem }, 0) catch return 1; eh.writeFile(path, out) catch return 1; @@ -495,6 +527,44 @@ fn runOne(arena: std.mem.Allocator, exe_z: [:0]const u8, script_path: []const u8 return 1; } +/// Every tape the script's sessions wrote, replayed by the same build +/// (PARDES_REPLAY), and by --replay-with's: why the first that diverged +/// did, or null. Another build's replay need agree only in its replica that +/// draws nothing, as a replica client draws none of its session's frames: +/// the two that draw meet a window's drawing where the session had a +/// terminal's. +fn replayTapes(arena: std.mem.Allocator, exe_z: [:0]const u8, stem: []const u8) ?[]const u8 { + if (replayTapesWith(arena, exe_z, stem, false)) |why| return why; + const other = replay_with orelse return null; + return replayTapesWith(arena, other, stem, true); +} + +fn replayTapesWith(arena: std.mem.Allocator, exe_z: [:0]const u8, stem: []const u8, headless: bool) ?[]const u8 { + const dir = tape_dir.?; + const io = std.Io.Threaded.global_single_threaded.io(); + var d = std.Io.Dir.cwd().openDir(io, dir, .{ .iterate = true }) catch return "the tape directory cannot be read"; + defer d.close(io); + var it = d.iterate(); + var tapes: usize = 0; + while (it.next(io) catch return "the tape directory cannot be listed") |entry| { + if (!std.mem.startsWith(u8, entry.name, stem) or !std.mem.endsWith(u8, entry.name, ".tape")) continue; + const pid = entry.name[stem.len..][0 .. entry.name.len - stem.len - ".tape".len]; + if (pid.len < 2 or pid[0] != '-' or std.mem.indexOfNone(u8, pid[1..], "0123456789") != null) continue; + tapes += 1; + const cmd = std.fmt.allocPrintSentinel(arena, "PARDES_RECORD= PARDES_REPLAY='{s}/{s}' {s}timeout 300 '{s}' > '{s}/{s}.replay' 2>&1", .{ dir, entry.name, if (dump_check and !headless) "PARDES_REPLAY_DUMP=1 " else "", exe_z, dir, entry.name }, 0) catch return "out of memory"; + if (system(cmd) != 0) { + const why_path = std.fmt.allocPrint(arena, "{s}/{s}.replay", .{ dir, entry.name }) catch return "out of memory"; + const said = readFileAlloc(arena, why_path) catch return "the replay failed and said nothing"; + if (headless) if (std.mem.indexOf(u8, said, ": headless replica: ")) |at| { + if (std.mem.startsWith(u8, std.mem.trimStart(u8, said[at + ": headless replica: ".len ..], "0123456789"), " digests agree")) continue; + }; + return said; + } + } + if (tapes == 0) return "no session wrote a tape"; + return null; +} + fn reportPath(arena: std.mem.Allocator, script_path: []const u8) ![]const u8 { return std.fmt.allocPrint(arena, "{s}/{s}.report", .{ SNAP_BASE, std.fs.path.stem(script_path) }); } @@ -603,12 +673,26 @@ fn runScript(arena: std.mem.Allocator, exe_z: [:0]const u8, script_path: []const // together. Scripts name it `$DUMP`. const dump_path = try std.fmt.allocPrint(arena, "{s}/dump.zon", .{base}); _ = setenv("PARDES_DUMP", try arena.dupeSentinel(u8, dump_path, 0), 1); + // Each session the script starts (and each pardes run inside one) + // writes its own tape, <stem>-<pid>.tape. + if (tape_dir) |dir| { + _ = system(try std.fmt.allocPrintSentinel(arena, "rm -f '{s}/{s}-'[0-9]*.tape '{s}/{s}-'[0-9]*.tape.replay", .{ dir, stem, dir, stem }, 0)); + _ = setenv("PARDES_RECORD", try std.fmt.allocPrintSentinel(arena, "{s}/{s}-", .{ dir, stem }, 0), 1); + } if (chdirRc(try arena.dupeSentinel(u8, work, 0)) < 0) return error.ChdirFailed; const chunks = try arena.alloc([]const u8, countCaptures(src)); var chunk_len: usize = 0; var h: ?eh.Harness = null; defer if (h) |*hp| hp.deinit(); + // `views=N`: the app's other terminals, and the one driven now. + var others: [3]eh.Harness = undefined; + var nothers: usize = 0; + var group: [4]*eh.Harness = undefined; + var cur: usize = 0; + defer for (others[0..nothers]) |*o| o.deinit(); + // A capture is a delta against the previous one of its terminal's. + var prevs: [4]Prev = @splat(.{}); // captures are deltas against the previous one of the same kind (see Prev) var prev: Prev = .{}; @@ -674,15 +758,46 @@ fn runScript(arena: std.mem.Allocator, exe_z: [:0]const u8, script_path: []const const cols = try std.fmt.parseInt(u16, tok.next() orelse return error.BadScript, 10); var extra: [6]?[*:0]const u8 = @splat(null); var nextra: usize = 0; - while (tok.next()) |a| : (nextra += 1) { + var nviews: usize = 1; + while (tok.next()) |a| { + if (std.mem.startsWith(u8, a, "views=")) { + nviews = try std.fmt.parseInt(usize, a["views=".len..], 10); + if (nviews < 1 or nviews > 4) return error.BadScript; + continue; + } if (nextra >= extra.len) return error.BadScript; extra[nextra] = (try arena.dupeSentinel(u8, a, 0)).ptr; + nextra += 1; } if (h) |*hp| hp.deinit(); // scripts may restart (e.g. dump then -l) + for (others[0..nothers]) |*o| o.deinit(); + nothers = 0; + cur = 0; // a new process is a new screen: capture it whole, or its first // frame would be a delta against a dead instance's rows prev = .{}; + prevs = @splat(.{}); + var paths: std.ArrayList(u8) = .empty; + while (nothers + 1 < nviews) : (nothers += 1) { + var path: [64]u8 = undefined; + others[nothers] = try eh.Harness.initView(gpa, rows, cols, &path); + try paths.print(arena, "{s}{s}", .{ if (paths.items.len > 0) " " else "", std.mem.sliceTo(&path, 0) }); + } + if (nothers > 0) _ = setenv("PARDES_TEST_VIEWS", try arena.dupeSentinel(u8, paths.items, 0), 1); h = try eh.Harness.initArgv(gpa, exe_z.ptr, rows, cols, extra[0..nextra]); + _ = unsetenv("PARDES_TEST_VIEWS"); + group[0] = &h.?; + for (others[0..nothers], 1..) |*o, i| { + o.pid = h.?.pid; + group[i] = o; + } + for (group[0 .. nothers + 1]) |v| v.views = group[0 .. nothers + 1]; + } else if (std.mem.eql(u8, cmd, "view")) { + const k = try std.fmt.parseInt(usize, tok.next() orelse return error.BadScript, 10); + if (k < 1 or k > nothers + 1) return error.BadScript; + prevs[cur] = prev; + cur = k - 1; + prev = prevs[cur]; } else if (std.mem.eql(u8, cmd, "snapfile")) { const label = tok.next() orelse return error.BadScript; const contents = try readFileAlloc(arena, tok.rest()); @@ -692,49 +807,52 @@ fn runScript(arena: std.mem.Allocator, exe_z: [:0]const u8, script_path: []const try appendChunk(chunks, &chunk_len, try snapFile(arena, label, contents)); } else if (std.mem.eql(u8, cmd, "wait")) { const ms = try std.fmt.parseInt(i64, tok.next() orelse return error.BadScript, 10); - try live(&h).expectWaitFor(tok.rest(), ms, "snapshot wait"); + try on(&h, &group, nothers, cur).expectWaitFor(tok.rest(), ms, "snapshot wait"); } else if (std.mem.eql(u8, cmd, "settle")) { - try live(&h).pump(try std.fmt.parseInt(i64, tok.next() orelse return error.BadScript, 10)); + try on(&h, &group, nothers, cur).pump(try std.fmt.parseInt(i64, tok.next() orelse return error.BadScript, 10)); } else if (std.mem.eql(u8, cmd, "stable")) { const quiet = try std.fmt.parseInt(i64, tok.next() orelse return error.BadScript, 10); const timeout = try std.fmt.parseInt(i64, tok.next() orelse return error.BadScript, 10); - try waitStable(live(&h), quiet, timeout); + try waitStable(on(&h, &group, nothers, cur), quiet, timeout); } else if (std.mem.eql(u8, cmd, "text")) { - try live(&h).send(tok.rest()); + try on(&h, &group, nothers, cur).send(tok.rest()); } else if (std.mem.eql(u8, cmd, "send")) { - try live(&h).send(try unescape(arena, tok.rest())); + try on(&h, &group, nothers, cur).send(try unescape(arena, tok.rest())); } else if (std.mem.eql(u8, cmd, "key")) { - while (tok.next()) |name| try live(&h).send(keyBytes(name) orelse return error.BadKey); + while (tok.next()) |name| try on(&h, &group, nothers, cur).send(keyBytes(name) orelse return error.BadKey); } else if (std.mem.eql(u8, cmd, "press") or std.mem.eql(u8, cmd, "release") or std.mem.eql(u8, cmd, "drag")) { const btn = buttonCode(tok.next() orelse return error.BadScript) orelse return error.BadScript; const col_spec = tok.next() orelse return error.BadScript; const row = try std.fmt.parseInt(u16, tok.next() orelse return error.BadScript, 10); - const col = try resolveCol(live(&h), col_spec, row); + const col = try resolveCol(on(&h, &group, nothers, cur), col_spec, row); const b: u16 = if (std.mem.eql(u8, cmd, "drag")) btn + 32 else btn; const fin: u8 = if (std.mem.eql(u8, cmd, "release")) 'm' else 'M'; - try live(&h).send(try std.fmt.allocPrint(arena, "\x1b[<{d};{d};{d}{c}", .{ b, col, row, fin })); + try on(&h, &group, nothers, cur).send(try std.fmt.allocPrint(arena, "\x1b[<{d};{d};{d}{c}", .{ b, col, row, fin })); } else if (std.mem.eql(u8, cmd, "motion")) { const col_spec = tok.next() orelse return error.BadScript; const row = try std.fmt.parseInt(u16, tok.next() orelse return error.BadScript, 10); - const col = try resolveCol(live(&h), col_spec, row); - try live(&h).send(try std.fmt.allocPrint(arena, "\x1b[<35;{d};{d}M", .{ col, row })); + const col = try resolveCol(on(&h, &group, nothers, cur), col_spec, row); + try on(&h, &group, nothers, cur).send(try std.fmt.allocPrint(arena, "\x1b[<35;{d};{d}M", .{ col, row })); } else if (std.mem.eql(u8, cmd, "wheel")) { const dir = tok.next() orelse return error.BadScript; const b: u16 = if (std.mem.eql(u8, dir, "up")) 64 else 65; const col_spec = tok.next() orelse return error.BadScript; const row = try std.fmt.parseInt(u16, tok.next() orelse return error.BadScript, 10); - const col = try resolveCol(live(&h), col_spec, row); - try live(&h).send(try std.fmt.allocPrint(arena, "\x1b[<{d};{d};{d}M", .{ b, col, row })); + const col = try resolveCol(on(&h, &group, nothers, cur), col_spec, row); + try on(&h, &group, nothers, cur).send(try std.fmt.allocPrint(arena, "\x1b[<{d};{d};{d}M", .{ b, col, row })); } else if (std.mem.eql(u8, cmd, "resize")) { const rows = try std.fmt.parseInt(u16, tok.next() orelse return error.BadScript, 10); const cols = try std.fmt.parseInt(u16, tok.next() orelse return error.BadScript, 10); - try live(&h).resize(rows, cols); + try on(&h, &group, nothers, cur).resize(rows, cols); + // Another view's terminal is no one's controlling one: the app + // is told it changed, as a stub tells it of its own. + if (cur > 0) std.posix.kill(live(&h).pid, std.posix.SIG.WINCH) catch {}; } else if (std.mem.eql(u8, cmd, "snap")) { - try appendChunk(chunks, &chunk_len, try snapText(arena, live(&h), tok.rest(), &prev)); + try appendChunk(chunks, &chunk_len, try snapText(arena, on(&h, &group, nothers, cur), tok.rest(), &prev)); } else if (std.mem.eql(u8, cmd, "snapstyle")) { - try appendChunk(chunks, &chunk_len, try snapStyle(arena, live(&h), tok.rest(), &prev)); + try appendChunk(chunks, &chunk_len, try snapStyle(arena, on(&h, &group, nothers, cur), tok.rest(), &prev)); } else if (std.mem.eql(u8, cmd, "snap9p")) { - try appendChunk(chunks, &chunk_len, try snap9p(arena, live(&h), tok.rest(), &prev)); + try appendChunk(chunks, &chunk_len, try snap9p(arena, on(&h, &group, nothers, cur), tok.rest(), &prev)); } else { std.debug.print("unknown command {s}\n", .{cmd}); return error.BadScript; @@ -848,6 +966,13 @@ fn snapFile(arena: std.mem.Allocator, label: []const u8, contents: []const u8) ! return out; } +/// The terminal the script drives now (`view`). +fn on(h: *?eh.Harness, group: *[4]*eh.Harness, nothers: usize, cur: usize) *eh.Harness { + const app = live(h); + if (cur == 0 or cur > nothers) return app; + return group[cur]; +} + fn live(h: *?eh.Harness) *eh.Harness { if (h.*) |*v| return v; fatal("script used the app before `start`", .{}); @@ -885,7 +1010,7 @@ fn waitStable(h: *eh.Harness, quiet: i64, timeout: i64) !void { // and nothing in its process tree is runnable. Held across two probes, // because a pane's shell can write and sleep a scheduler tick before // the app wakes to read it. - if (since >= probe_after_ms and h.pending() == 0 and treeAsleep(h.pid)) { + if (since >= probe_after_ms and h.pendingAll() == 0 and treeAsleep(h.pid)) { if (asleep_since == 0) { asleep_since = now; } else if (now - asleep_since >= probe_hold_ms) { diff --git a/test/snapshots/dump.golden b/test/snapshots/dump.golden index 2704918d..c9e8b73b 100644 --- a/test/snapshots/dump.golden +++ b/test/snapshots/dump.golden @@ -1,117 +1,24 @@ -== file dump -.{ - .magic = "pardes-dump", - .version = 1, - .screen = .{ .cols = 100, .rows = 31 }, - .active = 0, - .topbar = "Newcol Joincol Find Grep Help Changelog Tutor Dump Themes Config Debug Exit", - .topbar_custom = null, - .theme = "orchard", - .locations_config = .{ - .context = 0, - .tscontext = false, - .tslocations = true, - .layout = .stacked, - }, - .tree_context_tag_style = true, - .columns = .{ .{ - .serial = 1, - .tag = null, - .weight = 1, - .panes = .{ 0, 1 }, - }, .{ - .serial = 2, - .tag = null, - .weight = 1, - .panes = .{2}, - } }, - .panes = .{ - .{ - .kind = .terminal, - .tag = "/tmp/pardes-snap/dump/cwd Tty+bash Save Mode Filter Collapse Del", - .body = "ls\nd.txt\n\n\n\n typed-marker\n\n\n\n\n\n\n", - .scroll = 0, - .cols = 48, - .rows = 13, - .vweight = 1, - .collapsed = false, - .serial = 1, - .tag_tail = null, - .attached_to = null, - .terminal = .{ - .cwd = "/tmp/pardes-snap/dump/cwd", - .shell = "", - .command = "", - .status = null, - .stream = "ls\nd.txt\n", - .stream_b64 = <elided> - .stream_is_screen = true, - .cursor = .{ .col = 2, .row = 2 }, - .petscii = false, - }, - .file = null, - .image = null, - }, - .{ - .kind = .terminal, - .tag = "/tmp/pardes-snap/dump/cwd Tty+bash Save Mode Filter Collapse Del", - .body = "ls\nd.txt\n\n\n\n\n\n\n\n\n\n", - .scroll = 0, - .cols = 48, - .rows = 12, - .vweight = 1, - .collapsed = false, - .serial = 2, - .tag_tail = null, - .attached_to = null, - .terminal = .{ - .cwd = "/tmp/pardes-snap/dump/cwd", - .shell = "", - .command = "", - .status = null, - .stream = "ls\nd.txt\n", - .stream_b64 = <elided> - .stream_is_screen = true, - .cursor = .{ .col = 2, .row = 2 }, - .petscii = false, - }, - .file = null, - .image = null, - }, - .{ - .kind = .terminal, - .tag = "/tmp/pardes-snap/dump/cwd Tty+bash Save Mode Filter Collapse Del", - .body = "ls\nd.txt\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n", - .scroll = 0, - .cols = 48, - .rows = 27, - .vweight = 1, - .collapsed = false, - .serial = 3, - .tag_tail = null, - .attached_to = null, - .terminal = .{ - .cwd = "/tmp/pardes-snap/dump/cwd", - .shell = "", - .command = "", - .status = null, - .stream = "ls\nd.txt\n", - .stream_b64 = <elided> - .stream_is_screen = true, - .cursor = .{ .col = 2, .row = 2 }, - .petscii = false, - }, - .file = null, - .image = null, - }, - }, - .mounts = .{}, - .settings = .{ - "MessageAnimation off", - "MessageLinger 0", - "Shell bash", - }, - .incl = .{ "/usr/include", "/usr/local/include" }, - .carried = null, -} -== end file +== snap loaded grid=100x31 cursor=16,9 +| Newcol Joincol Find Grep Help Changelog Tutor Dump Themes Config Debug Exit +| New Tty Find Grep Joincol Delcol New Tty Find Grep Joincol Delcol +| /tmp/pardes-snap/dump/cwd Tty+bash Save Mode Fil /tmp/pardes-snap/dump/cwd Tty+bash Save Mode Fil +| ter Collapse Del ter Collapse Del +| ls Dump ls +| d.txt d.txt +| +| ── restored history ── ── restored history ── +| +| typed-marker +| +| +| +| +| +| +| +| /tmp/pardes-snap/dump/cwd Tty+bash Save Mode Fil +| ter Collapse Del +| ls +| d.txt +| +| ── restored history ── diff --git a/test/snapshots/dump.snap b/test/snapshots/dump.snap index 29e7b09e..5a602304 100644 --- a/test/snapshots/dump.snap +++ b/test/snapshots/dump.snap @@ -1,4 +1,6 @@ -# Dump serializes the session to this fixture's $DUMP path. +# Dump writes the whole session to this fixture's $DUMP path, a dump of the +# walk (src/walk.zig), and `-l` of it is that session again: the text typed +# into a terminal over its history comes back where it was. file d.txt x start 31 100 -n 3 wait 8000 d.txt @@ -14,4 +16,7 @@ stable 400 5000 press middle @Dump 1 release middle @Dump 1 stable 600 8000 -snapfile dump $DUMP +start 31 100 -l $DUMP +wait 8000 typed-marker +stable 700 20000 +snap loaded diff --git a/test/snapshots/headersweep-restored.golden b/test/snapshots/headersweep-restored.golden index 522945bd..64e48503 100644 --- a/test/snapshots/headersweep-restored.golden +++ b/test/snapshots/headersweep-restored.golden @@ -4,7 +4,7 @@ |2: 0-1 d,#add38a, 2-43 #becfc4,#314738, 44-52 #dfbce5,#314738, 53-59 #becfc4,#314738, 60-61 #becfc4,#496656, 62-103 #b2c3b8,#1d2824, 104-112 #d3b0d9,#1d2824, 113-119 #b2c3b8,#1d2824, |3: 0-1 d,#314738, 2-59 #becfc4,#314738, 60-61 #becfc4,#1d2824, 62-119 #b2c3b8,#1d2824, |4: 0-0 d,#608269, 1-1 d,#0d1410, 2-59 #e5eee5,#0d1410, 60-60 #e5eee5,#608269, 61-119 #e5eee5,#0d1410, -|5: 0-0 d,#608269, 1-1 d,#0d1410, 2-59 #e5eee5,#0d1410, 60-60 #e5eee5,#608269, 61-119 #e5eee5,#0d1410, +|5: 0-0 d,#608269, 1-59 d,#0d1410, 60-60 d,#608269, 61-119 d,#0d1410, |6: 0-0 d,#608269, 1-1 d,#0d1410, 2-23 #e5eee5,#0d1410,f 24-59 #e5eee5,#0d1410, 60-60 #e5eee5,#608269, 61-61 #e5eee5,#0d1410, 62-83 #e5eee5,#0d1410,f 84-119 #e5eee5,#0d1410, |7: 0-0 d,#608269, 1-59 d,#0d1410, 60-60 d,#608269, 61-119 d,#0d1410, |8: 0-0 d,#608269, 1-59 d,#0d1410, 60-60 d,#608269, 61-119 d,#0d1410, @@ -14,7 +14,7 @@ |12: 0-1 d,#496656, 2-43 #b2c3b8,#1d2824, 44-52 #d3b0d9,#1d2824, 53-59 #b2c3b8,#1d2824, 60-60 #b2c3b8,#608269, 61-119 #b2c3b8,#0d1410, |13: 0-1 d,#1d2824, 2-59 #b2c3b8,#1d2824, 60-60 #b2c3b8,#608269, 61-119 #b2c3b8,#0d1410, |14: 0-0 d,#608269, 1-1 d,#0d1410, 2-59 #e5eee5,#0d1410, 60-60 #e5eee5,#608269, 61-119 #e5eee5,#0d1410, -|15: 0-0 d,#608269, 1-1 d,#0d1410, 2-59 #e5eee5,#0d1410, 60-60 #e5eee5,#608269, 61-119 #e5eee5,#0d1410, +|15: 0-0 d,#608269, 1-59 d,#0d1410, 60-60 d,#608269, 61-119 d,#0d1410, |16: 0-0 d,#608269, 1-1 d,#0d1410, 2-23 #e5eee5,#0d1410,f 24-59 #e5eee5,#0d1410, 60-60 #e5eee5,#608269, 61-119 #e5eee5,#0d1410, |17: 0-0 d,#608269, 1-59 d,#0d1410, 60-60 d,#608269, 61-119 d,#0d1410, |18: 0-0 d,#608269, 1-59 d,#0d1410, 60-60 d,#608269, 61-119 d,#0d1410, @@ -25,8 +25,8 @@ | New Tty Find Grep Joincol Delcol echo RE''STORED-COL New Tty Find Grep Joincol Delcol | /tmp/pardes-snap/headersweep-restored/cwd Tty+bash Save Mo /tmp/pardes-snap/headersweep-restored/cwd Tty+bash Save Mo | de Filter Collapse Del de Filter Collapse Del -| $ ls $ ls -| $ $ +| ls ls +| | ── restored history ── ── restored history ── | | @@ -35,7 +35,7 @@ | | /tmp/pardes-snap/headersweep-restored/cwd Tty+bash Save Mo | de Filter Collapse Del -| $ +| | ── restored history ── | | /tmp/pardes-snap/headersweep-restored/cwd (echo RE''STORED @@ -44,6 +44,7 @@ == style ws-sweeping grid=120x21 |0: 0-1 d,#1d2824, 2-100 #b2c3b8,#1d2824, 101-108 #f5f7ee,#5b7e5a, 109-119 #b2c3b8,#1d2824, |1: 0-1 d,#887455, 2-59 #becfc4,#314738, 60-61 #becfc4,#887455, 62-119 #b2c3b8,#1d2824, +|14: 0-0 d,#608269, 1-59 d,#0d1410, 60-60 d,#608269, 61-119 d,#0d1410, |15: 0-0 d,#608269, 1-1 d,#0d1410, 2-23 #e5eee5,#0d1410,f 24-59 #e5eee5,#0d1410, 60-60 #e5eee5,#608269, 61-119 #e5eee5,#0d1410, |16: 0-0 d,#152019, 1-59 d,#0d1410, 60-60 d,#608269, 61-119 d,#0d1410, |17: 0-1 d,#496656, 2-59 #b2c3b8,#1d2824, 60-60 #b2c3b8,#608269, 61-119 #b2c3b8,#0d1410, diff --git a/test/snapshots/load.golden b/test/snapshots/load.golden index 18de9a8f..ea8f26ec 100644 --- a/test/snapshots/load.golden +++ b/test/snapshots/load.golden @@ -3,48 +3,48 @@ | New Tty Find Grep Joincol Delcol New Tty Find Grep Joincol Delcol | /tmp/pardes-snap/load/cwd Tty+bash Save Mode Fil /tmp/pardes-snap/load/cwd Tty+bash Save Mode Fil | ter Collapse Del ter Collapse Del -| $ echo LOAD''MARK; seq 1 40; echo E''ND $ ls -| LOADMARK d.txt -| 1 $ -| 2 ── restored history ── -| 3 -| 4 -| 5 -| 6 -| 7 -| 8 -| 9 -| 10 -| 11 +| 28 Dump ls +| 29 d.txt +| 30 +| 31 ── restored history ── +| 32 +| 33 +| 34 +| 35 +| 36 +| 37 +| 38 +| 39 +| 40 | /tmp/pardes-snap/load/cwd Tty+bash Save Mode Fil | ter Collapse Del -| $ ls +| ls | d.txt -| $ +| | ── restored history ── == style loaded grid=100x31 -|0: 0-1 d,#1d2824, 2-99 #b2c3b8,#1d2824, +|0: 0-1 d,#1d2824, 2-47 #b2c3b8,#1d2824, 48-51 #f5f7ee,#3c614a, 52-52 #f5f7ee,#1d2824, 53-99 #b2c3b8,#1d2824, |1: 0-1 d,#887455, 2-49 #becfc4,#314738, 50-51 #becfc4,#887455, 52-99 #b2c3b8,#1d2824, |2: 0-1 d,#add38a, 2-27 #becfc4,#314738, 28-36 #dfbce5,#314738, 37-49 #becfc4,#314738, 50-51 #becfc4,#496656, 52-77 #b2c3b8,#1d2824, 78-86 #d3b0d9,#1d2824, 87-99 #b2c3b8,#1d2824, |3: 0-1 d,#314738, 2-49 #becfc4,#314738, 50-51 #becfc4,#1d2824, 52-99 #b2c3b8,#1d2824, -|4: 0-0 d,#608269, 1-1 d,#0d1410, 2-49 #e5eee5,#0d1410, 50-50 #e5eee5,#608269, 51-99 #e5eee5,#0d1410, -|5: 0-0 d,#608269, 1-1 d,#0d1410, 2-49 #e5eee5,#0d1410, 50-50 #e5eee5,#608269, 51-99 #e5eee5,#0d1410, -|6: 0-0 d,#608269, 1-1 d,#0d1410, 2-49 #e5eee5,#0d1410, 50-50 #e5eee5,#608269, 51-99 #e5eee5,#0d1410, +|4: 0-0 d,#152019, 1-1 d,#0d1410, 2-43 #e5eee5,#0d1410, 44-44 #e5eee5,#1d2824, 45-49 #b2c3b8,#1d2824, 50-50 #b2c3b8,#608269, 51-51 #b2c3b8,#0d1410, 52-99 #e5eee5,#0d1410, +|5: 0-0 d,#152019, 1-1 d,#0d1410, 2-49 #e5eee5,#0d1410, 50-50 #e5eee5,#608269, 51-99 #e5eee5,#0d1410, +|6: 0-0 d,#152019, 1-1 d,#0d1410, 2-49 #e5eee5,#0d1410, 50-50 #e5eee5,#608269, 51-99 #e5eee5,#0d1410, |7: 0-0 d,#152019, 1-1 d,#0d1410, 2-49 #e5eee5,#0d1410, 50-50 #e5eee5,#608269, 51-51 #e5eee5,#0d1410, 52-73 #e5eee5,#0d1410,f 74-99 #e5eee5,#0d1410, |8: 0-0 d,#152019, 1-1 d,#0d1410, 2-49 #e5eee5,#0d1410, 50-50 #e5eee5,#608269, 51-99 #e5eee5,#0d1410, |9: 0-0 d,#152019, 1-1 d,#0d1410, 2-49 #e5eee5,#0d1410, 50-50 #e5eee5,#608269, 51-99 #e5eee5,#0d1410, |10: 0-0 d,#152019, 1-1 d,#0d1410, 2-49 #e5eee5,#0d1410, 50-50 #e5eee5,#608269, 51-99 #e5eee5,#0d1410, |11: 0-0 d,#152019, 1-1 d,#0d1410, 2-49 #e5eee5,#0d1410, 50-50 #e5eee5,#608269, 51-99 #e5eee5,#0d1410, -|12: 0-0 d,#152019, 1-1 d,#0d1410, 2-49 #e5eee5,#0d1410, 50-50 #e5eee5,#608269, 51-99 #e5eee5,#0d1410, -|13: 0-0 d,#152019, 1-1 d,#0d1410, 2-49 #e5eee5,#0d1410, 50-50 #e5eee5,#608269, 51-99 #e5eee5,#0d1410, -|14: 0-0 d,#152019, 1-1 d,#0d1410, 2-49 #e5eee5,#0d1410, 50-50 #e5eee5,#608269, 51-99 #e5eee5,#0d1410, +|12: 0-0 d,#608269, 1-1 d,#0d1410, 2-49 #e5eee5,#0d1410, 50-50 #e5eee5,#608269, 51-99 #e5eee5,#0d1410, +|13: 0-0 d,#608269, 1-1 d,#0d1410, 2-49 #e5eee5,#0d1410, 50-50 #e5eee5,#608269, 51-99 #e5eee5,#0d1410, +|14: 0-0 d,#608269, 1-1 d,#0d1410, 2-49 #e5eee5,#0d1410, 50-50 #e5eee5,#608269, 51-99 #e5eee5,#0d1410, |15: 0-0 d,#152019, 1-1 d,#0d1410, 2-49 #e5eee5,#0d1410, 50-50 #e5eee5,#608269, 51-99 #e5eee5,#0d1410, |16: 0-0 d,#152019, 1-1 d,#0d1410, 2-49 #e5eee5,#0d1410, 50-50 #e5eee5,#608269, 51-99 #e5eee5,#0d1410, |17: 0-1 d,#496656, 2-27 #b2c3b8,#1d2824, 28-36 #d3b0d9,#1d2824, 37-49 #b2c3b8,#1d2824, 50-50 #b2c3b8,#608269, 51-99 #b2c3b8,#0d1410, |18: 0-1 d,#1d2824, 2-49 #b2c3b8,#1d2824, 50-50 #b2c3b8,#608269, 51-99 #b2c3b8,#0d1410, |19: 0-0 d,#608269, 1-1 d,#0d1410, 2-49 #e5eee5,#0d1410, 50-50 #e5eee5,#608269, 51-99 #e5eee5,#0d1410, |20: 0-0 d,#608269, 1-1 d,#0d1410, 2-49 #e5eee5,#0d1410, 50-50 #e5eee5,#608269, 51-99 #e5eee5,#0d1410, -|21: 0-0 d,#608269, 1-1 d,#0d1410, 2-49 #e5eee5,#0d1410, 50-50 #e5eee5,#608269, 51-99 #e5eee5,#0d1410, +|21: 0-0 d,#608269, 1-49 d,#0d1410, 50-50 d,#608269, 51-99 d,#0d1410, |22: 0-0 d,#608269, 1-1 d,#0d1410, 2-23 #e5eee5,#0d1410,f 24-49 #e5eee5,#0d1410, 50-50 #e5eee5,#608269, 51-99 #e5eee5,#0d1410, |23: 0-0 d,#608269, 1-49 d,#0d1410, 50-50 d,#608269, 51-99 d,#0d1410, |24: 0-0 d,#608269, 1-49 d,#0d1410, 50-50 d,#608269, 51-99 d,#0d1410, diff --git a/test/snapshots/load.snap b/test/snapshots/load.snap index 62c661b7..75a9a341 100644 --- a/test/snapshots/load.snap +++ b/test/snapshots/load.snap @@ -1,7 +1,6 @@ -# pardes -l restores another instance's dump (NEW behavior — the old app had a -# separate replay viewer; this golden is generated from the NEW binary as its -# own regression baseline). Shell output survives the replay; typed insert runs -# are an overlay and do not (same semantics as the old replay viewer). +# pardes -l loads another instance's dump: the session as it was dumped, each +# terminal's view where it was (scrolled back into the seq output), its +# history above a "restored history" banner and a new shell below it. file d.txt x start 31 100 -n 3 wait 8000 d.txt @@ -21,7 +20,7 @@ press middle @Dump 1 release middle @Dump 1 stable 600 8000 start 31 100 -l $DUMP -wait 8000 LOADMARK +wait 8000 restored stable 700 20000 snap loaded snapstyle loaded diff --git a/test/snapshots/restore.golden b/test/snapshots/restore.golden index a8e39308..0a6fd913 100644 --- a/test/snapshots/restore.golden +++ b/test/snapshots/restore.golden @@ -7,6 +7,5 @@ | KEEPME | $ == snap restored grid=100x31 cursor=4,8 -|2: /tmp/pardes-snap/restore/cwd Tty+bash Save Mode Filter Collapse Del -|3: $ ls |7: ── restored history ── +|8: $ diff --git a/test/snapshots/tagnav.golden b/test/snapshots/tagnav.golden index e7c6eca4..7ed268f5 100644 --- a/test/snapshots/tagnav.golden +++ b/test/snapshots/tagnav.golden @@ -37,12 +37,11 @@ == snap topbar-exec grid=100x31 cursor=7,4 |0: Newcol Joincol Find Grep Help Changelog Tutor Dump Themes Config Debug Exit Restore /tmp/pardes-sn |4: 1 alpha Dump ls -== snap topbar-restore-word grid=100x31 cursor=78,0 +== snap topbar-restore-word grid=100x31 cursor=60,0 +|0: d Grep Help Changelog Tutor Dump Themes Config Debug Exit Restore /tmp/pardes-snap/tagnav/dump.zon |4: 1 alpha ls == snap topbar-restored grid=100x31 cursor=7,4 -|4: 1 alpha $ ls -|6: 3 gamma $ +|0: Newcol Joincol Find Grep Help Changelog Tutor Dump Themes Config Debug Exit Restore /tmp/pardes-sn +|4: 1 alpha Dump ls |7: /tmp/pardes-snap/tagnav/cwd Tty+bash Save Mode F ── restored history ── -|9: $ ls -|11: $ |12: ── restored history ── diff --git a/test/snapshots/views-pan.golden b/test/snapshots/views-pan.golden new file mode 100644 index 00000000..db63d29c --- /dev/null +++ b/test/snapshots/views-pan.golden @@ -0,0 +1,52 @@ +== snap tall grid=80x24 cursor=4,16 +| Newcol Joincol Find Grep Help Changelog Tutor Dump Themes Config Debug Exit +| New Tty Find Grep Joincol Delcol +|$ /tmp/pardes-snap/views-pan/cwd Tty+bash Save Mode Filter Collapse Del +| 18 +| 19 +| 20 +| 21 +| 22 +| 23 +| 24 +| 25 +| 26 +| 27 +| 28 +| 29 +| 30 +| $ +| /tmp/pardes-snap/views-pan/cwd/+New Save Tty Collapse Del +| 1 +== snap short-pans grid=60x12 cursor=4,8 +| Newcol Joincol Find Grep Help Changelog Tutor Dump Themes +| New Tty Find Grep Joincol Delcol +|$ /tmp/pardes-snap/views-pan/cwd Tty+bash Save Mode Filter C +| ollapse Del +|↑ 27 +| 28 +| 29 +| 30 +| $ +| /tmp/pardes-snap/views-pan/cwd/+New Save Tty Collapse Del +| 1 +== snap short-typed grid=60x12 cursor=4,8 +|4: 29 +|5: 30 +|6: $ echo typed-in-short +|7: typed-in-short +== snap tall-sees grid=80x24 cursor=4,7 +|3: 29 +|4: 30 +|5: $ echo typed-in-short +|6: typed-in-short +|7: $ +|8: +|9: +|10: +|11: +|12: +|13: +|14: +|15: +|16: diff --git a/test/snapshots/views-pan.snap b/test/snapshots/views-pan.snap new file mode 100644 index 00000000..3ba1ac5b --- /dev/null +++ b/test/snapshots/views-pan.snap @@ -0,0 +1,27 @@ +# A terminal shown in a view shorter than it (src/views.zig): sized for the +# tall view that typed into it, the short one passes over its top rows so +# its cursor and prompt stay in sight, an arrow in the gutter saying rows +# are above. +config Verbose off +start 24 80 views=2 +wait 8000 $ +stable 700 20000 +view 2 +resize 12 60 +stable 500 10000 +view 1 +text seq 1 30 +key enter +wait 8000 30 +stable 500 10000 +snap tall +view 2 +stable 500 10000 +snap short-pans +text echo typed-in-short +key enter +stable 500 10000 +snap short-typed +view 1 +stable 500 10000 +snap tall-sees diff --git a/test/snapshots/views-term.golden b/test/snapshots/views-term.golden new file mode 100644 index 00000000..d3cff726 --- /dev/null +++ b/test/snapshots/views-term.golden @@ -0,0 +1,46 @@ +== snap small grid=50x14 cursor=4,4 +| Newcol Joincol Find Grep Help Changelog Tutor Du +| New Tty Find Grep Joincol Delcol +|$ /tmp/pardes-snap/views-term/cwd Tty+bash Save Mo +| de Filter Collapse Del +| $ +| +| +| +| +| +| /tmp/pardes-snap/views-term/cwd/+New Save Tty Co +| llapse Del +| 1 +== snap big grid=80x24 cursor=4,5 +| Newcol Joincol Find Grep Help Changelog Tutor Dump Themes Config Debug Exit +| New Tty Find Grep Joincol Delcol +|$ /tmp/pardes-snap/views-term/cwd Tty+bash Save Mode Filter Collapse Del +| $ echo from-the-big-one +| from-the-big-one +| $ +| +| +| +| +| +| +| +| +| +| +| +| /tmp/pardes-snap/views-term/cwd/+New Save Tty Collapse Del +| 1 +== snap small-sees grid=50x14 cursor=4,6 +|4: $ echo from-the-big-one +|5: from-the-big-one +|6: $ +== snap small-typed grid=50x14 cursor=4,8 +|6: $ echo from-the-small-one +|7: from-the-small-one +|8: $ +== snap big-sees grid=80x24 cursor=4,7 +|5: $ echo from-the-small-one +|6: from-the-small-one +|7: $ diff --git a/test/snapshots/views-term.snap b/test/snapshots/views-term.snap new file mode 100644 index 00000000..bbd1c368 --- /dev/null +++ b/test/snapshots/views-term.snap @@ -0,0 +1,27 @@ +# A terminal shown in two views at two sizes (src/views.zig): it takes the +# size of the view that typed into it last; the other shows it at that size. +config Verbose off +start 24 80 views=2 +wait 8000 $ +stable 700 20000 +view 2 +resize 14 50 +stable 500 10000 +snap small +# Typed in the big view: sized for it, shown so in the small one. +view 1 +text echo from-the-big-one +key enter +stable 500 10000 +snap big +view 2 +stable 500 10000 +snap small-sees +# Typed in the small view: sized for it now, the big one shows it so. +text echo from-the-small-one +key enter +stable 500 10000 +snap small-typed +view 1 +stable 500 10000 +snap big-sees diff --git a/test/snapshots/views.golden b/test/snapshots/views.golden new file mode 100644 index 00000000..40f6a294 --- /dev/null +++ b/test/snapshots/views.golden @@ -0,0 +1,123 @@ +== snap one grid=80x24 cursor=7,3 +| Newcol Joincol Find Grep Help Changelog Tutor Dump Themes Config Debug Exit +| New Tty Find Grep Joincol Delcol +| /tmp/pardes-snap/views/cwd/v.txt Save Tty Collapse Del +| 1 line 1 +| 2 line 2 +| 3 line 3 +| 4 line 4 +| 5 line 5 +| 6 line 6 +| 7 line 7 +| 8 line 8 +| 9 line 9 +| 10 line 10 +| 11 line 11 +| 12 line 12 +| 13 line 13 +| 14 line 14 +| 15 line 15 +| 16 line 16 +| 17 line 17 +| 18 line 18 +| 19 line 19 +| 20 line 20 +| 21 line 21 +== snap two grid=80x24 cursor=7,3 +| Newcol Joincol Find Grep Help Changelog Tutor Dump Themes Config Debug Exit +| New Tty Find Grep Joincol Delcol +| /tmp/pardes-snap/views/cwd/v.txt Save Tty Collapse Del +| 1 line 1 +| 2 line 2 +| 3 line 3 +| 4 line 4 +| 5 line 5 +| 6 line 6 +| 7 line 7 +| 8 line 8 +| 9 line 9 +| 10 line 10 +| 11 line 11 +| 12 line 12 +| 13 line 13 +| 14 line 14 +| 15 line 15 +| 16 line 16 +| 17 line 17 +| 18 line 18 +| 19 line 19 +| 20 line 20 +| 21 line 21 +== snap one-edit grid=80x24 cursor=15,3 +|2: */tmp/pardes-snap/views/cwd/v.txt Save Tty Collapse Del +|3: 1 EDIT-ONEline 1 +== snap two-sees grid=80x24 cursor=7,3 +|2: */tmp/pardes-snap/views/cwd/v.txt Save Tty Collapse Del +|3: 1 EDIT-ONEline 1 +== snap two-down grid=80x24 cursor=7,6 +|3: 22 line 22 +|4: 23 line 23 +|5: 24 line 24 +|6: 25 line 25 +|7: 26 line 26 +|8: 27 line 27 +|9: 28 line 28 +|10: 29 line 29 +|11: 30 line 30 +|12: 31 line 31 +|13: 32 line 32 +|14: 33 line 33 +|15: 34 line 34 +|16: 35 line 35 +|17: 36 line 36 +|18: 37 line 37 +|19: 38 line 38 +|20: 39 line 39 +|21: 40 line 40 +|22: 41 line 41 +|23: 42 line 42 +== snap one-top grid=80x24 cursor=15,3 +== snap two-put grid=80x24 cursor=21,7 +|7: 26 EDIT-ONEline 1 +|8: 27 line 26 +|9: 28 line 27 +|10: 29 line 28 +|11: 30 line 29 +|12: 31 line 30 +|13: 32 line 31 +|14: 33 line 32 +|15: 34 line 33 +|16: 35 line 34 +|17: 36 line 35 +|18: 37 line 36 +|19: 38 line 37 +|20: 39 line 38 +|21: 40 line 39 +|22: 41 line 40 +|23: 42 line 41 +== snap two-col grid=80x24 cursor=42,1 +|1: New Tty Find Grep Joincol Delcol New Tty Find Grep Joincol Delcol +|2: */tmp/pardes-snap/views/cwd/v.txt Save +|3: Tty Collapse Del +|4: 22 line 22 +|5: 23 line 23 +|6: 24 line 24 +|7: 25 line 25 +|8: 26 EDIT-ONEline 1 +|9: 27 line 26 +|10: 28 line 27 +|11: 29 line 28 +|12: 30 line 29 +|13: 31 line 30 +|14: 32 line 31 +|15: 33 line 32 +|16: 34 line 33 +|17: 35 line 34 +|18: 36 line 35 +|19: 37 line 36 +|20: 38 line 37 +|21: 39 line 38 +|22: 40 line 39 +|23: 41 line 40 +== snap one-col grid=80x24 cursor=21,3 +== snap one-after grid=80x24 cursor=13,4 diff --git a/test/snapshots/views.snap b/test/snapshots/views.snap new file mode 100644 index 00000000..eba26bae --- /dev/null +++ b/test/snapshots/views.snap @@ -0,0 +1,57 @@ +# Two terminals on one session, each a view of it (src/views.zig): each its +# own layout, focus, cursor and scroll over the same panes. An edit typed in +# one is in the other; a line yanked in one is put in the other (registers +# are the session's); Newcol in one leaves the other's columns; Detach in +# one lets that terminal go and leaves the other drawing. +config Verbose off +lines v.txt 120 +start 24 80 views=2 v.txt +wait 8000 line 1 +stable 500 10000 +snap one +view 2 +wait 8000 line 1 +stable 500 10000 +snap two +# Typed in view 1: in view 2 too. +view 1 +key i +text EDIT-ONE +key esc +settle 100 +stable 400 8000 +snap one-edit +view 2 +stable 400 8000 +snap two-sees +# Scrolled in view 2: view 1 stays at the top. +key pgdn +stable 400 8000 +snap two-down +view 1 +stable 400 8000 +snap one-top +# Yanked in view 1, put in view 2. +key x y +stable 400 8000 +view 2 +key p +stable 400 8000 +snap two-put +# A column in view 2 only. +key space +text cn +stable 400 8000 +snap two-col +view 1 +stable 400 8000 +snap one-col +# Detached in view 2: its terminal let go, view 1 drawing on. +view 2 +key space +text sD +stable 400 8000 +view 1 +key j +stable 400 8000 +snap one-after diff --git a/test/soak.py b/test/soak.py new file mode 100644 index 00000000..e3ff18c7 --- /dev/null +++ b/test/soak.py @@ -0,0 +1,1815 @@ +#!/usr/bin/env python3 +"""A soak of replica clients: what a person does in them, through each link, +measured (docs/next-daemon.md, the replica and GUI replica notes). + + python3 -B test/soak.py local [--minutes 2] [--rtts 0,30,80,207] [--jitter 10] + [--seed 1] [--clients tty,win] [--pardes P] [--gui G] [--trace] [--keep] + python3 -B test/soak.py box [--minutes 20] [--clients win,tty] [--remote-bin B] + [--pardes P] [--gui G] [--seed 1] [--trace] [--keep] + + python3 -B test/soak.py instant [--rtts 0,30,80,207 | --box] [--clients tty,win] + +The gates (`source .scratch/daemon/hunt/env.sh` or any env without PARDES_*): + local, ~12 min: python3 -B test/soak.py local --minutes 2.5 + box, ~22 min: python3 -B test/soak.py box --minutes 20 + instant, ~5 min a cell: python3 -B test/soak.py instant --rtts 207 + python3 -B test/soak.py instant --box + +`instant` asks whether each local action feels local (mosh's point): per action +class (type in a file, type at a shell prompt, move by keys and by click, scroll +a file by keys and the wheel, scroll a terminal's history by keys and the +wheel, select, type into the Panes picker) and client kind, 25 inputs each in +one client a kind, against a local session's own frontend measured first in the +same run. Its columns: `at once`, the share applied at once (none of them +stopped at a host call, by the replica's own trace, and each shown sooner than +half a round trip); p50/p99 keystroke to present; the local session's p99; +`none`, inputs that showed nothing in 2 s; for type_shell the keys whose echo +was predicted. Breaches: a local-only class under 99% at once, or its p99 more +than two frames over the local one; shell echo predicted under 95%; more than +one input showing nothing. The soak's own breach for blink: visible rollbacks at +1 a minute or more. +(the box's binary is deployed first as /root/pardes-test/bin/pardes-hunt, the +same build as --pardes/--gui here). Exit 0 only with no breach at all. + +Narrowing a breach down: `--trace` (PARDES_REPLICA_TRACE at both ends: per-field +digests, each unit and each divergence described in the clients' and the +session's logs), `--only move,insert` / `--skip click,hover` (the monkey's +actions by name, ACTIONS below), `--events resize,detach` ('' for none), +`--no-upgrade`, `--no-respawn` (a dead client stays dead, a dead session ends +the cell), `--near` (local clients without zstd and blobs by hash), `--keep` +(the run's files, with each terminal client's output and the actions log, a +line an input). A breach's files are kept whatever. + +`local`: a session on this machine (`pardes --detach=soak <a directory>`, the +shape of the user's: a directory pane, then two shells), and at each RTT its +clients through test/delay_relay.py (with jitter), far as through ssh +(PARDES_REPLICA_FAR=1: the blobs by hash, the stream in zstd). One cell an +RTT, `--minutes` each. `box`: the session on the FreeBSD box under +/root/pardes-test (`--remote-bin`, by default bin/pardes-hunt there, run in +run-hunt and home-hunt), the clients here through ssh, as the user's window +was; its 9P through an `ssh -L` of the session's socket. + +The clients: terminals (`pardes --attach`, in a pty here whose screen pyte +keeps, frame requests answered) and windows (`pardes-gui --attach`, SDL's +offscreen driver, keys and the mouse through its test feed on a pty, so it +can be resized). Each runs a seeded monkey of a person's actions: moves of +every kind (keys, clicks, jumps, search), scrolls (keys, the wheel), edits, +undo and save, Look at files and directories, the Panes picker, Find, new +terminals with programs (ls, a long output, less, vi, top, Ctrl-C), panes and +columns opened, moved and closed, the pointer resting and moving, pastes, +idle gaps. Between them, at random: a client resized, a client detached and +another attached, an Upgrade, a 9P script's edit. `--seed` makes a run +again (the timing differs, the actions do not). + +Measured (a row a client, then the session's): + - rejoins: links past the first (PARDES_REPLICA_STATS writes one a link) + and `joined again` said by it; checks (PARDES_REPLICA_CHECK) that differ; + - exits nobody asked for, a signal or a non-zero code, the session's death, + a panic or `crashes` line; + - freezes: a quiet point where a client gives no fresh check within 8 s, + and a terminal client's key with no output within 2 s while it was not + held; the session's worst boundary (its stats at its end); + - the view: at each quiet point, a terminal client's cursor and screen + (pyte) against what the session renders for its view (/screen read right + after its own input made its view the live one), and the panes on it; + - blink: visible rollbacks (`visible`), mispredicted inputs, a terminal + client's full clears, a minute; + - keystroke to output (terminal) and to a frame (window, PARDES_TEST_LATENCY) + p50 / p99. + +Prints a table, then each breach, and exits 1 if there is any. Everything +started is ended by pid (and found by its environment); the run's files are +kept under $TMPDIR/soak-* (`--keep`) or removed. +""" +import argparse +import fcntl +import json +import os +import random +import re +import shutil +import signal +import socket +import statistics +import struct +import subprocess +import sys +import tempfile +import termios +import threading +import time +import tty as ttymod +from pathlib import Path + +import pyte + +HERE = Path(__file__).resolve().parent +sys.path.insert(0, str(HERE)) +from ninep import Client # noqa: E402 +from daemon import processes, alive, wait_for, checks # noqa: E402 + +BOX = '[email protected]' +SSH = ['/usr/bin/ssh', '-F', '/dev/null', '-o', 'BatchMode=yes', '-o', 'IdentitiesOnly=yes', + '-o', 'UserKnownHostsFile=/home/goblin/.ssh/known_hosts', '-i', '/home/goblin/.ssh/id_ed25519'] +BOX_T = '/root/pardes-test' +NAME = 'soak' +ESC = b'\x1b' + + +def now(): + return time.monotonic() + + +def pct(xs, p): + if not xs: + return None + xs = sorted(xs) + return xs[min(len(xs) - 1, len(xs) * p // 100)] + + +# ---- the fixture: a directory such as a person works in ---- + +WORDS = ('alpha bravo charlie delta echo foxtrot golf hotel india juliet kilo lima mike november oscar papa ' + 'romeo sierra tango uniform victor whiskey xray yankee zulu').split() + + +def fixture(at, rng): + at.mkdir(parents=True, exist_ok=True) + (at / 'notes.txt').write_text(''.join(f'line {i} of the notes: {rng.choice(WORDS)} {rng.choice(WORDS)} see code.zig:{i % 90 + 1}\n' + for i in range(300))) + (at / 'code.zig').write_text(''.join(f'pub fn f{i}(x: u32) u32 {{\n return x * {i} + {rng.randrange(1000)}; // {rng.choice(WORDS)}\n}}\n\n' + for i in range(120))) + (at / 'big.txt').write_text(''.join(f'{i:06d} {" ".join(rng.choice(WORDS) for _ in range(9))}\n' for i in range(8000))) + (at / 'README').write_text('A directory to soak in.\nnotes.txt code.zig big.txt sub/\nsub/inner.txt:3\n') + sub = at / 'sub' + sub.mkdir(exist_ok=True) + (sub / 'inner.txt').write_text(''.join(f'inner {i} {rng.choice(WORDS)}\n' for i in range(60))) + (sub / 'more.md').write_text('# More\n\n' + ''.join(f'- item {i} {rng.choice(WORDS)}\n' for i in range(40))) + + +# ---- terminals ---- + +class Term: + """A terminal: a pty whose output is kept, timed and fed to pyte, its + frame requests (CSI 5 n) answered as a terminal emulator does. An Esc is + written alone and nothing follows it for 80 ms, an answer included: the + client's parser takes an Esc and what follows within 50 ms as Alt.""" + + def __init__(self, rows, cols): + self.master, self.slave = os.openpty() + self.rows, self.cols = rows, cols + fcntl.ioctl(self.slave, termios.TIOCSWINSZ, struct.pack('HHHH', rows, cols, 0, 0)) + self.lock = threading.Lock() + self.wlock = threading.Lock() + self.screen = pyte.Screen(cols, rows) + self.stream = pyte.ByteStream(self.screen) + self.out_bytes = 0 + self.last_out = 0.0 + self.chunks = [] # (time, bytes) of the last minute or so, for latencies + self.clears = 0 + self.joined_again = 0 + self.tail = b'' + self.esc_until = 0.0 + self.closing = False + self.log = None + self.reader = threading.Thread(target=self.drain, daemon=True) + self.reader.start() + + def drain(self): + import select + while not self.closing: + try: + if not select.select([self.master], [], [], 0.05)[0]: + continue + got = os.read(self.master, 65536) + except OSError: + return + if not got: + return + t = now() + carry, self.tail = self.tail, (self.tail + got)[-11:] + seen = carry[-3:] + got + with self.lock: + self.out_bytes += len(got) + self.last_out = t + self.chunks.append((t, len(got))) + if len(self.chunks) > 4096: + del self.chunks[:2048] + # The carry is shorter than either word: a match holds a new byte. + self.clears += seen.count(b'\x1b[2J') + self.joined_again += (carry + got).count(b'joined again') + self.feed(got) + # A rejoin's notice on the screen, counted as it comes up. + for word, attr in ((' diverged from the session', 'shown_diverged'), ('is upgrading', 'shown_upgrade')): + up = any(word in line for line in self.screen.display) + if up and not getattr(self, attr + '_on', False): + setattr(self, attr, getattr(self, attr, 0) + 1) + setattr(self, attr + '_on', up) + if self.log: + self.log.write(got) + if b'\x1b[5n' in seen: + wait = self.esc_until - now() + if wait > 0: + time.sleep(wait) + with self.wlock: + try: + os.write(self.master, b'\x1b[0n') + except OSError: + return + + SGR = re.compile(rb'\x1b\[[0-9:;]*m') + + def feed(self, got): + """To pyte, without its colours (pyte reads no colon sub-parameters) + and holding back a sequence a read cut in two.""" + data = getattr(self, 'held_back', b'') + got + cut = data.rfind(b'\x1b') + self.held_back = b'' + if cut >= 0 and not re.match(rb'\x1b(\[[0-9:;?<>=! ]*[@-~]|[^\[\]]|\][^\x07\x1b]*(\x07|\x1b\\))', data[cut:]): + data, self.held_back = data[:cut], data[cut:] + try: + self.stream.feed(self.SGR.sub(b'', data)) + except Exception: + pass + + def write(self, data): + """Keys as typed: each Esc alone, 80 ms clear after it.""" + parts = re.split(b'(\x1b(?![\\[O\\]n0-9a-z]))', data) + for part in parts: + if not part: + continue + if part == ESC: + with self.wlock: + wait = self.esc_until - now() + if wait > 0: + time.sleep(wait) + os.write(self.master, ESC) + self.esc_until = now() + 0.08 + time.sleep(0.08) + else: + with self.wlock: + wait = self.esc_until - now() + if wait > 0: + time.sleep(wait) + os.write(self.master, part) + + def click(self, button, x, y): + w, h = self.px + px, py = int(x * w), int(y * h) + self.inputs += 1 + self.run.note(f'{self.label} < click {button} {px} {py}') + with self.wlock: + try: + os.write(self.master, f'\x1b]777;mouse;down;{button};{px};{py}\x07\x1b]777;mouse;up;{button};{px};{py}\x07'.encode()) + except OSError: + pass + + def resize(self, rows, cols): + self.rows, self.cols = rows, cols + self.px = (cols * 16, rows * 33) + fcntl.ioctl(self.master, termios.TIOCSWINSZ, struct.pack('HHHH', rows, cols, 0, 0)) + with self.lock: + self.screen.resize(rows, cols) + + def view(self): + with self.lock: + return [line.rstrip() for line in self.screen.display], (self.screen.cursor.y, self.screen.cursor.x), self.screen.cursor.hidden + + def spawn(self, argv, env, cwd, err): + slave = self.slave + return subprocess.Popen(argv, env=env, cwd=cwd, stdin=slave, stdout=slave, stderr=err, start_new_session=True, + preexec_fn=lambda: fcntl.ioctl(0, termios.TIOCSCTTY, 0)) + + def close(self): + self.closing = True + self.reader.join(timeout=2) + for fd in (self.slave, self.master): + try: + os.close(fd) + except OSError: + pass + + +# ---- clients ---- + +class Base: + def __init__(self, run, kind, label, link): + self.run = run + self.kind = kind + self.label = label + self.link = link # where it dials: a socket path, or the box + self.dir = run.root / label + self.dir.mkdir() + self.check = self.dir / 'check' + self.stats = self.dir / 'stats' + self.err = self.dir / 'err' + self.p = None + self.started = None + self.asked_to_end = False + self.gone = None # how it ended, if it did + self.inputs = 0 + self.lat = [] # ms, input to output/frame + self.no_output = 0 + self.held = threading.Event() # the monkey waits while set + self.paused = threading.Event() # set by the monkey once it waits + self.lock = threading.Lock() + + def env(self): + e = dict(self.run.env, PARDES_REPLICA_CHECK=str(self.check), PARDES_REPLICA_STATS=str(self.stats)) + # Its own machine's state (Recent, the store), as a client elsewhere has. + (self.dir / 'state').mkdir(exist_ok=True) + e['XDG_STATE_HOME'] = str(self.dir / 'state') + if self.run.args.trace: + e['PARDES_REPLICA_TRACE'] = '1' + if self.run.far: + e['PARDES_REPLICA_FAR'] = '1' + return e + + def attach_arg(self): + return f'--attach={self.link}:{NAME}' + + def links(self): + return [line for line in checks(self.stats) if line.startswith('inputs ')] + + def stats_sum(self): + keys = ('inputs', 'at_once', 'stopped', 'units', 'fast', 'neutral', 'mispredicted', 'foreign', 'visible', 'missed') + tot = dict.fromkeys(keys, 0) + tot['echoed'] = tot['typed'] = 0 + for line in self.links(): + w = line.split() + d = {w[i]: w[i + 1] for i in range(0, len(w) - 1, 2)} + a, _, b = d.get('echo', '0/0').partition('/') + tot['echoed'] += int(a or 0) + tot['typed'] += int(b or 0) + for k in keys: + try: + # `visible` is the link's (its process's) count so far, the + # rest each replica's own. + tot[k] = max(tot[k], int(d.get(k, 0))) if k == 'visible' else tot[k] + int(d.get(k, 0)) + except ValueError: + pass + return tot + + def check_lines(self): + return checks(self.check) + + def poll(self): + if self.gone is None and self.p is not None and self.p.poll() is not None: + self.gone = self.p.returncode + return self.gone + + +class TtyClient(Base): + def __init__(self, run, label, link, rows=30, cols=100): + super().__init__(run, 'tty', label, link) + self.term = Term(rows, cols) + self.term.log = open(self.dir / 'out', 'wb', buffering=0) if run.args.keep else None + + def start(self): + argv = [self.run.pardes, self.attach_arg()] + # Its stderr a file: a panic's words and the replica's trace. + self.p = self.term.spawn(argv, self.env(), self.run.root, open(self.err, 'w')) + self.started = now() + + def ready(self): + return self.term.out_bytes > 2000 and now() - self.term.last_out > 0.3 + + def keys(self, data): + self.inputs += 1 + self.run.note(f'{self.label} < {data!r}') + before = self.term.out_bytes + t = now() + self.term.write(data) + threading.Thread(target=self._latency, args=(t, before), daemon=True).start() + + def _latency(self, t, before): + deadline = t + 2.0 + while now() < deadline: + with self.term.lock: + got = [c for c in self.term.chunks if c[0] >= t] + if got: + with self.lock: + self.lat.append((got[0][0] - t) * 1000) + return + time.sleep(0.005) + if not self.held.is_set(): + with self.lock: + self.no_output += 1 + self.run.note(f'{self.label} no output in 2 s after the key at {t - self.run.t0:.3f}') + + def mouse(self, kind, button, x, y): + # SGR: cells from 1; x, y here are fractions of the screen. + col = max(1, min(self.term.cols, int(x * self.term.cols) + 1)) + row = max(1, min(self.term.rows, int(y * self.term.rows) + 1)) + b = {'down': button - 1, 'up': button - 1, 'motion': 35, 'wheel': 64 if button > 0 else 65}[kind] + end = 'm' if kind == 'up' else 'M' + self.keys(f'\x1b[<{b};{col};{row}{end}'.encode()) + + def click(self, button, x, y): + col = max(1, min(self.term.cols, int(x * self.term.cols) + 1)) + row = max(1, min(self.term.rows, int(y * self.term.rows) + 1)) + self.keys(f'\x1b[<{button - 1};{col};{row}M\x1b[<{button - 1};{col};{row}m'.encode()) + + def resize(self, rows, cols): + self.term.resize(rows, cols) + if self.p: + try: + os.killpg(self.p.pid, signal.SIGWINCH) + except OSError: + pass + + def said(self): + try: + return self.err.read_text(errors='replace') + except FileNotFoundError: + return '' + + def said_joined(self): + return self.term.joined_again + + def rejoins(self): + """Joins again: (after a divergence, after an Upgrade's refusal). A + terminal's notice can be gone before a frame shows it: the Upgrades + made while it ran are counted as its refusals.""" + end = getattr(self, 'ended_at', None) or now() + up = max(getattr(self.term, 'shown_upgrade', 0), sum(1 for t in self.run.upgrade_times if self.started <= t <= end)) + return max(getattr(self.term, 'shown_diverged', 0), len(self.links()) - 1 - up), up + + def hangup(self): + """As its terminal closing: the normal way out (its stats written).""" + try: + os.killpg(self.p.pid, signal.SIGHUP) + except OSError: + pass + + def close(self): + self.term.close() + + +class WinClient(Base): + """A window client in test mode: offscreen, its keys and pointer through + its test feed on a pty (Escape as CSI 27 u: a lone ESC waits for more).""" + + def __init__(self, run, label, link, rows=34, cols=110): + super().__init__(run, 'win', label, link) + self.cap = self.dir / 'cap' + self.cap.mkdir() + self.latency = self.dir / 'latency' + self.master, self.slave = os.openpty() + ttymod.setraw(self.slave) + self.rows, self.cols = rows, cols + fcntl.ioctl(self.master, termios.TIOCSWINSZ, struct.pack('HHHH', rows, cols, 0, 0)) + self.wlock = threading.Lock() + self.px = (cols * 16, rows * 33) # test mode's 27 px font, about + + def start(self): + e = dict(self.env(), PARDES_TEST='1', PARDES_TEST_CAPTURE_DIR=str(self.cap), PARDES_TEST_LATENCY=str(self.latency), + SDL_VIDEO_DRIVER='offscreen', PARDES_TEST_COLS=str(self.cols), PARDES_TEST_ROWS=str(self.rows)) + self.p = subprocess.Popen([self.run.gui, self.attach_arg()], env=e, cwd=self.run.root, stdin=self.slave, + stdout=subprocess.DEVNULL, stderr=open(self.err, 'w'), start_new_session=True) + self.started = now() + + def ready(self): + # Software presenting (offscreen) captures no ppm: its first frame is + # its latency trace's first F. + try: + return b'F ' in self.latency.read_bytes() and self.p.poll() is None + except FileNotFoundError: + return False + + def keys(self, data): + self.inputs += 1 + self.run.note(f'{self.label} < {data!r}') + with self.wlock: + try: + os.write(self.master, data.replace(ESC + b'[', b'\x00CSI').replace(ESC, b'\x1b[27u').replace(b'\x00CSI', ESC + b'[')) + except OSError: + pass + + def mouse(self, kind, button, x, y): + w, h = self.px + px, py = int(x * w), int(y * h) + if kind == 'wheel': + seq = f'\x1b]777;mouse;wheel;{button};{px};{py}\x07' + else: + seq = f'\x1b]777;mouse;{kind};{button};{px};{py}\x07' + self.inputs += 1 + self.run.note(f'{self.label} < mouse {kind} {button} {px} {py}') + with self.wlock: + try: + os.write(self.master, seq.encode()) + except OSError: + pass + + def click(self, button, x, y): + w, h = self.px + px, py = int(x * w), int(y * h) + self.inputs += 1 + self.run.note(f'{self.label} < click {button} {px} {py}') + with self.wlock: + try: + os.write(self.master, f'\x1b]777;mouse;down;{button};{px};{py}\x07\x1b]777;mouse;up;{button};{px};{py}\x07'.encode()) + except OSError: + pass + + def resize(self, rows, cols): + self.rows, self.cols = rows, cols + self.px = (cols * 16, rows * 33) + fcntl.ioctl(self.master, termios.TIOCSWINSZ, struct.pack('HHHH', rows, cols, 0, 0)) + + def hangup(self): + """Its test feed's end: the normal way out (its stats written).""" + try: + os.close(self.master) + except OSError: + pass + + def said(self): + try: + return self.err.read_text(errors='replace') + except FileNotFoundError: + return '' + + def said_joined(self): + return self.said().count('joined again') + + def rejoins(self): + said = self.said() + return said.count('joined again: this client diverged'), said.count('joined again: the session is upgrading') + + def frame_size(self): + try: + with open(self.cap / 'latest.ppm', 'rb') as f: + head = f.read(32).split() + self.px = (int(head[1]), int(head[2])) + except (OSError, IndexError, ValueError): + pass + + def latencies(self): + """Each stdin read to the end of the first frame after it, ms; and the + reads no frame followed within 2 s (a frame came after a later one).""" + try: + lines = self.latency.read_text().split('\n') + except FileNotFoundError: + return [], 0 + reads, frames = [], [] + for line in lines: + w = line.split() + if len(w) >= 2 and w[0] == 'R': + reads.append(int(w[1])) + elif len(w) >= 3 and w[0] == 'F': + frames.append(int(w[2])) + out, missed, j = [], 0, 0 + for r in reads: + while j < len(frames) and frames[j] < r: + j += 1 + if j < len(frames): + d = (frames[j] - r) / 1e6 + if d <= 2000: + out.append(d) + else: + missed += 1 + return out, missed + + def close(self): + for fd in (self.slave, self.master): + try: + os.close(fd) + except OSError: + pass + + +# ---- what a person does ---- + +def words(rng, n): + return ' '.join(rng.choice(WORDS) for _ in range(n)).encode() + + +def burst(c, rng): + """One action of a person's, in whatever state the client is.""" + k = c.keys + acts = c.run.actions + choice = rng.choices(acts, weights=[a[1] for a in acts])[0] + choice[2](c, rng, k) + return choice[0] + + +def act_move(c, rng, k): + for _ in range(rng.randint(1, 6)): + k(rng.choice([b'j', b'k', b'h', b'l', b'w', b'b', b'e', b'5j', b'3k', b'gg', b'ge', b'\x04', b'\x15', + b'\x06', b'\x02', b'}', b'{', b'%', b'x', b'12G', b'gl', b'gh'])) + time.sleep(rng.uniform(0.02, 0.2)) + + +def act_search(c, rng, k): + k(b'/' + rng.choice(WORDS).encode() + b'\r') + for _ in range(rng.randint(0, 3)): + time.sleep(rng.uniform(0.1, 0.4)) + k(rng.choice([b'n', b'N'])) + + +def act_jump(c, rng, k): + k(rng.choice([b'\x0f', b' jo', b' jj', b'gd', b'\x0f\x0f'])) + + +def act_insert(c, rng, k): + k(rng.choice([b'i', b'a', b'o', b'O', b'A'])) + for w in words(rng, rng.randint(1, 5)).split(): + k(w + b' ') + time.sleep(rng.uniform(0.03, 0.15)) + k(ESC) + + +def act_delete(c, rng, k): + k(rng.choice([b'xd', b'd', b'wd', b'xxd', b'c' + words(rng, 1) + ESC])) + + +def act_undo(c, rng, k): + k(rng.choice([b'u', b'uu', b'U', b'u'])) + + +def act_save(c, rng, k): + k(b' fs') + + +def act_look(c, rng, k): + # Enter is Look: a directory's row opens it, a word finds its next place. + k(rng.choice([b'\r', b'j\r', b'jj\r', b'gg\r'])) + + +def act_panes(c, rng, k): + k(b' bb') + time.sleep(rng.uniform(0.3, 1.0)) + k(rng.choice([b'\r', ESC, b'jj\r', b'not\r'])) + + +def act_find(c, rng, k): + k(rng.choice([b' ff', b' fr', b' fg'])) + time.sleep(rng.uniform(0.3, 1.0)) + k(rng.choice([b'notes\r', ESC, b'j\r', b'inner\r'])) + + +def act_shell(c, rng, k): + # A terminal of its own, a program in it, and back out to normal mode. + k(b' nt') + time.sleep(rng.uniform(0.8, 1.5)) + for _ in range(rng.randint(1, 3)): + prog = rng.choice(['ls -la\r', 'seq 1 3000\r', 'less big.txt\r', 'vi notes.txt\r', 'top\r', 'sleep 30\r', + 'echo ' + rng.choice(WORDS) + '\r', 'cat code.zig\r']) + k(prog.encode()) + time.sleep(rng.uniform(0.5, 2.0)) + if prog.startswith('less'): + # Never SPC then q: that is Exit, should the keys reach no less. + k(b'jjjj\x06') + time.sleep(0.3) + k(b'q') + elif prog.startswith('vi'): + k(b'jjjjx') + k(ESC) + k(b':q!\r') + elif prog.startswith(('top', 'sleep', 'seq')): + k(b'\x03' if not prog.startswith('top') else b'q') + time.sleep(rng.uniform(0.2, 0.8)) + k(b'\x02') # Ctrl-b: normal mode + if rng.random() < 0.5 and c.run.pane_count() >= 4: + time.sleep(0.3) + k(b' d') + time.sleep(0.6) + k(rng.choice([b'k', b'j', b''])) + + +def act_window(c, rng, k): + # Enter ends window mode, as a person would: left open, it took the next + # action's keys, and a `k` at a column's top went to its tag, where `SPC q` ran Exit + k(b'\x17' + rng.choice([b'h', b'j', b'k', b'l']) + b'\r') + + +def act_column(c, rng, k): + few = c.run.pane_count() < 5 + k(rng.choice([b' cn', b' cn'] if few else [b' cn', b' cn', b' cd', b' cj'])) + + +def act_del(c, rng, k): + if c.run.pane_count() < 4: + return act_move(c, rng, k) + k(b' d') + time.sleep(0.6) + k(rng.choice([b'k', b'j', b'', b' d'])) + + +def act_new(c, rng, k): + k(b' fn') + time.sleep(0.4) + k(b'i' + words(rng, 4)) + k(ESC) + + +def act_esc(c, rng, k): + k(rng.choice([ESC, b'\x02', b':', b':'])) + time.sleep(0.3) + k(ESC) + + +def act_click(c, rng, k): + x, y = rng.random(), rng.uniform(0.05, 1.0) + b = rng.choice([1, 1, 1, 3]) + c.mouse('down', b, x, y) + time.sleep(rng.uniform(0.03, 0.12)) + if rng.random() < 0.3: # a drag + c.mouse('motion', b, min(1, x + rng.uniform(0, 0.2)), min(1, y + rng.uniform(0, 0.1))) + c.mouse('up', b, x, y) + + +def act_wheel(c, rng, k): + x, y = rng.random(), rng.uniform(0.1, 1.0) + d = rng.choice([1, -1]) + for _ in range(rng.randint(1, 8)): + c.mouse('wheel', d, x, y) + time.sleep(rng.uniform(0.01, 0.08)) + + +def act_hover(c, rng, k): + x, y = rng.random(), rng.uniform(0.1, 1.0) + for _ in range(rng.randint(2, 12)): + x = min(1, max(0, x + rng.uniform(-0.05, 0.05))) + y = min(1, max(0.05, y + rng.uniform(-0.03, 0.03))) + c.mouse('motion', 0, x, y) + time.sleep(rng.uniform(0.01, 0.06)) + time.sleep(rng.uniform(0.05, 0.6)) # resting: a Look preview + + +def act_paste(c, rng, k): + k(b'i\x1b[200~' + b'\n'.join(words(rng, 6) for _ in range(rng.randint(1, 4))) + b'\x1b[201~') + k(ESC) + + +def act_settings(c, rng, k): + k(rng.choice([b' tw', b' ?', b' hm', b' tt'])) + time.sleep(0.5) + k(ESC) + + +def act_idle(c, rng, k): + time.sleep(rng.choice([0.5, 1, 2, 4, 8])) + + +ACTIONS = [ + ('move', 30, act_move), ('search', 5, act_search), ('jump', 4, act_jump), ('insert', 12, act_insert), + ('delete', 4, act_delete), ('undo', 5, act_undo), ('save', 3, act_save), ('look', 6, act_look), + ('panes', 3, act_panes), ('find', 3, act_find), ('shell', 3, act_shell), ('window', 4, act_window), + ('column', 1, act_column), ('del', 2, act_del), ('new', 2, act_new), ('esc', 4, act_esc), + ('click', 8, act_click), ('wheel', 8, act_wheel), ('hover', 6, act_hover), ('paste', 2, act_paste), + ('settings', 1, act_settings), ('idle', 6, act_idle), +] + + +def monkey(c, rng, stop): + while not stop.is_set(): + if c.held.is_set(): + c.paused.set() + time.sleep(0.05) + continue + c.paused.clear() + if c.poll() is not None: + return + try: + what = burst(c, rng) + c.run.note(f'{c.label} {what}') + except OSError: + return + time.sleep(rng.uniform(0.05, 0.6)) + + +# ---- the run ---- + +class Run: + def __init__(self, args, cell): + self.args = args + self.cell = cell + self.root = Path(tempfile.mkdtemp(prefix=f'soak-{cell}-', dir=os.environ.get('TMPDIR') or '/tmp')) + self.pardes = str(Path(args.pardes).resolve()) + self.gui = str(Path(args.gui).resolve()) + self.far = True + self.pids = [] + self.clients = [] + self.relays = [] + self.events = [] + self.breaches = [] + self.notes = open(self.root / 'actions', 'w') + only = set(args.only.split(',')) if args.only else None + skip = set(args.skip.split(',')) if args.skip else set() + self.actions = [a for a in ACTIONS if (only is None or a[0] in only) and a[0] not in skip] + self.nlock = threading.Lock() + self.t0 = now() + env = {k: v for k, v in os.environ.items() if not k.startswith(('PARDES_', 'WAYLAND', 'DBUS_', 'DISPLAY', 'BROWSER')) + and 'PAGER' not in k and k not in ('winid', 'EDITOR', 'NINE_MOUNT')} + env.update(HOME=str(self.root), XDG_RUNTIME_DIR=str(self.root), XDG_CONFIG_HOME=str(self.root / 'config'), + XDG_CACHE_HOME=str(self.root / 'cache'), XDG_STATE_HOME=str(self.root / 'state'), + TERM='xterm-256color', SHELL=shutil.which('bash') or '/bin/bash', PARDES_NOTIME='1') + (self.root / 'config' / 'pardes').mkdir(parents=True) + (self.root / 'config' / 'pardes' / 'init').write_text('Shell bash\nMessageLinger 0\n') + self.env = env + self.work = self.root / 'work' + self.nine = None + self.nine_lock = threading.RLock() + self.session_pid = None + self.epochs = 0 + self.made = [0] + self.upgrade_times = [] + # What the session opens (its work directory, or a file in it) and + # the shells made beside it. + self.opens = '' + self.shells = 2 + + def reset_session(self): + """What a dead session left (its daemon, its shells, its sockets) gone, + for the next to start clean.""" + for pid in processes(self.root): + if pid not in [c.p.pid for c in self.clients if c.p and c.p.poll() is None]: + try: + os.kill(pid, signal.SIGKILL) + except OSError: + pass + time.sleep(0.5) + for f in self.root.glob('pardes-*'): + if f.is_socket() or f.name.endswith('.lock'): + f.unlink(missing_ok=True) + if self.nine: + try: + self.nine.socket.close() + except Exception: + pass + self.nine = None + + def note(self, what): + with self.nlock: + self.notes.write(f'{now() - self.t0:9.3f} {what}\n') + self.notes.flush() + + def breach(self, what): + self.note('BREACH ' + what) + self.breaches.append(what) + + def spawn(self, argv, **kw): + p = subprocess.Popen(argv, **kw) + self.pids.append(p.pid) + return p + + def relay(self, rtt): + if not rtt: + return self.root / 'pardes-replica.sock' + at = self.root / f'relay{rtt}.sock' + if not at.exists(): + self.relays.append(self.spawn([sys.executable, '-B', str(HERE / 'delay_relay.py'), str(at), str(self.root / 'pardes-replica.sock'), + '--delay', str(rtt / 2), '--jitter', str(self.args.jitter)], + stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)) + wait_for('the relay', at.exists) + return at + + def safe_tags(self): + """The workspace tag without Exit, Dump and the like: the monkey's keys + land in tags too, and an Enter there runs what the line names (a person + does not type Exit by chance; a monkey does, given minutes).""" + self.nine_write('/tag', b'Newcol Joincol Find Grep Help Themes') + + def pane_count(self): + """The session's panes (a script's view of /index), cached a second: + a Del that closed the last one would end the session.""" + t = now() + if t - getattr(self, 'counted_at', 0) > 1: + rows = self.nine_read('/index') or b'' + self.counted, self.counted_at = len(rows.splitlines()), t + return self.counted + + def nine_read(self, path): + with self.nine_lock: + return self._nine_read(path) + + def nine_write(self, path, data): + with self.nine_lock: + return self._nine_write(path, data) + + def _nine_read(self, path): + for _ in range(3): + try: + if self.nine is None: + self.nine = Client(self.nine_sock, timeout=10) + return self.nine.read(path) + except (OSError, EOFError): + try: + self.nine.socket.close() + except Exception: + pass + self.nine = None + time.sleep(0.5) + return None + + def _nine_write(self, path, data): + for _ in range(2): + try: + if self.nine is None: + self.nine = Client(self.nine_sock, timeout=10) + return self.nine.write(path, data) + except EOFError: + self.nine = None + time.sleep(0.5) + except OSError as e: + return e + + # The session: here. + def start_session(self): + rng = random.Random(self.args.seed) + fixture(self.work, rng) + e = dict(self.env, PARDES_REPLICA_STATS=str(self.root / 'session.stats')) + if self.args.trace: + e['PARDES_REPLICA_TRACE'] = '1' + self.session = self.spawn([self.pardes, f'--detach={NAME}', str(self.work / self.opens)], env=e, cwd=self.work, + stdin=subprocess.DEVNULL, stdout=subprocess.DEVNULL, stderr=open(self.root / f'session.err.{self.epochs}', 'w'), + start_new_session=True) + self.nine_sock = self.root / f'pardes-9p-{NAME}.sock' + wait_for('the session\'s 9P socket', self.nine_sock.exists, timeout=20) + wait_for('the replica socket', (self.root / 'pardes-replica.sock').exists, timeout=20) + status = self.nine_read('/status').decode() + self.session_pid = int(dict(line.split(maxsplit=1) for line in status.splitlines())['pid']) + self.pids.append(self.session_pid) + self.safe_tags() + # Two shells beside the directory, as the user's had. + for _ in range(self.shells): + self.nine_write('/pane/1/exec', b'Tty\n') + time.sleep(0.5) + + def session_alive(self): + return alive(self.session_pid) if self.session_pid else False + + def end_session(self): + """Exit (twice: unsaved text asks once), then --kill if it stays.""" + for _ in range(3): + self.nine_write('/ctl', b'Exit\n') + try: + wait_for('the session\'s end', lambda: not self.session_alive(), timeout=3) + return True + except TimeoutError: + pass + self.note('the session did not Exit: --kill') + self.kill_session() + return False + + def kill_session(self): + subprocess.run([self.pardes, f'--kill={NAME}'], env=self.env, cwd=self.root, capture_output=True, timeout=30) + + def session_said(self): + return ''.join(f.read_text(errors='replace') for f in sorted(self.root.glob('session.err.*'))) + + def crashes(self): + p = self.root / 'config' / 'pardes' / 'crashes' + return p.read_text() if p.exists() else '' + + def cleanup(self): + for c in self.clients: + if c.p and c.p.poll() is None: + c.p.kill() + c.close() + for pid in self.pids + processes(self.root): + try: + os.kill(pid, signal.SIGKILL) + except OSError: + pass + if self.nine: + try: + self.nine.socket.close() + except Exception: + pass + + +class BoxRun(Run): + """The session on the FreeBSD box; the clients here through ssh.""" + + def __init__(self, args, cell): + super().__init__(args, cell) + self.far = False # an ssh client is far by itself + self.run_dir = f'{BOX_T}/run-hunt' + self.home = f'{BOX_T}/home-hunt' + self.env['PARDES_SSH'] = ' '.join(SSH) + self.env['PARDES_REMOTE_RUNTIME'] = self.run_dir + self.fwd = None + + def box(self, script, timeout=60): + return subprocess.run(SSH + [BOX, script], capture_output=True, timeout=timeout) + + def benv(self): + return (f'HOME={self.home} XDG_RUNTIME_DIR={self.run_dir} XDG_CONFIG_HOME={self.home}/config TMPDIR={BOX_T}/tmp-hunt ' + f'XDG_CACHE_HOME={self.home}/cache XDG_STATE_HOME={self.home}/state PATH={BOX_T}/bin:/usr/local/bin:/usr/bin:/bin ' + f'SHELL=/usr/local/bin/bash TERM=xterm-256color PARDES_NOTIME=1 PARDES_REPLICA_STATS={self.home}/session.stats' + + (' PARDES_REPLICA_TRACE=1' if self.args.trace else '')) + + def start_session(self): + rng = random.Random(self.args.seed) + fixture(self.work, rng) + tar = subprocess.run(['tar', '-C', str(self.work), '-cf', '-', '.'], capture_output=True, check=True).stdout + bin_ = self.args.remote_bin + fresh = f'rm -rf {self.home}; ' if self.epochs <= 1 else f'mkdir -p {self.home}/cores; mv {self.home}/work/*.core {self.home}/cores/ 2>/dev/null || true; ' + script = (f'set -e; [ -d {self.run_dir} ] && [ -n "$(ls {self.run_dir} 2>/dev/null)" ] && {{ echo "run-hunt busy"; ls {self.run_dir}; exit 3; }}; ' + + fresh + f'mkdir -p {self.home}/work {self.home}/config/pardes {BOX_T}/tmp-hunt {self.run_dir}; chmod 700 {self.run_dir}; ' + f'printf "Shell bash\\nMessageLinger 0\\n" > {self.home}/config/pardes/init; ' + f'tar -C {self.home}/work -xf -; cd {self.home}/work; ' + f'env -i {self.benv()} {bin_} --detach={NAME} {self.home}/work/{self.opens} </dev/null >{self.home}/session.err.{self.epochs} 2>&1 & ' + f'for i in $(seq 400); do [ -S {self.run_dir}/pardes-9p-{NAME}.sock ] && break; sleep 0.05; done; ' + f'[ -S {self.run_dir}/pardes-9p-{NAME}.sock ] || {{ cat {self.home}/session.err.{self.epochs}; exit 4; }}') + got = subprocess.run(SSH + [BOX, script], input=tar, capture_output=True, timeout=120) + if got.returncode != 0: + raise RuntimeError(f'box session: {got.returncode} {got.stdout.decode()} {got.stderr.decode()}') + self.nine_sock = self.root / 'box9p.sock' + self.nine_sock.unlink(missing_ok=True) + self.fwd = self.spawn(SSH + ['-N', '-o', 'StreamLocalBindUnlink=yes', '-L', f'{self.nine_sock}:{self.run_dir}/pardes-9p-{NAME}.sock', BOX], + stdin=subprocess.DEVNULL, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL) + wait_for('the 9P forward', self.nine_sock.exists, timeout=20) + status = wait_for('the session over the 9P forward', lambda: self.nine_read('/status'), timeout=30).decode() + self.session_pid = int(dict(line.split(maxsplit=1) for line in status.splitlines())['pid']) + self.safe_tags() + for _ in range(self.shells): + self.nine_write('/pane/1/exec', b'Tty\n') + time.sleep(1) + + def session_alive(self): + # An ssh that stalls (the box's link, 30 s once in a 20-minute run) is no session's + # death: ask again, and only a no is one (a box gone for good fails the next checks) + for _ in range(3): + try: + got = self.box(f'kill -0 {self.session_pid} 2>/dev/null && echo yes || echo no', timeout=30) + except subprocess.TimeoutExpired: + self.note('harness: the box took 30 s to say whether the session lives; asking again') + continue + return got.stdout.strip() == b'yes' + return True + + def session_said(self): + got = self.box(f'cat {self.home}/session.err.*; ls {self.home}/cores/*.core {self.home}/work/*.core 2>/dev/null; cat {self.home}/config/pardes/crashes 2>/dev/null', timeout=60) + return got.stdout.decode(errors='replace') + + def crashes(self): + return '' + + def kill_session(self): + self.box(f'env -i {self.benv()} {self.args.remote_bin} --kill={NAME}', timeout=60) + + def reset_session(self): + self.kill_remote() + if self.fwd and self.fwd.poll() is None: + self.fwd.kill() + if self.nine: + try: + self.nine.socket.close() + except Exception: + pass + self.nine = None + + def relay(self, rtt): + return BOX + + def cleanup(self): + super().cleanup() + self.kill_remote() + + def kill_remote(self): + # Everything started under run-hunt, by its environment: ps -e prints it. + self.box(f'for p in $(ps -axeww -o pid= -o command= | grep "XDG_RUNTIME_DIR={self.run_dir}" | grep -v grep | awk \'{{print $1}}\'); do kill -9 $p; done; ' + f'sleep 0.5; rm -rf {self.run_dir}/*', timeout=60) + + +def start_client(run, kind, rtt, n): + label = f'{kind}{rtt}-{n}' + link = run.relay(rtt) + c = (TtyClient if kind == 'tty' else WinClient)(run, label, link) + c.rtt = rtt + c.start() + run.pids.append(c.p.pid) + run.clients.append(c) + run.note(f'{label} started (pid {c.p.pid})') + wait_for(f'{label}\'s first frame', c.ready, timeout=60 if isinstance(run, BoxRun) else 30) + if isinstance(c, WinClient): + c.frame_size() + time.sleep(0.3) + return c + + +def hold(run, clients, timeout=30): + for c in clients: + c.held.set() + deadline = now() + timeout + for c in clients: + while not c.paused.is_set() and c.poll() is None and now() < deadline: + time.sleep(0.05) + + +def release(clients): + for c in clients: + c.held.clear() + + +def quiet_point(run, live): + """Every monkey held: each client must give a fresh check within 8 s + (a freeze otherwise), and none may differ; a terminal client's view + against the session's.""" + hold(run, live) + time.sleep(1.5) + for c in live: + if c.poll() is not None: + continue + before = len(c.check_lines()) + # A key that moves nothing a digest sees but the view's cursor, so the + # client's input is the session's last: its view is the live one. + c.keys(b'l') + t = now() + fresh = False + while now() - t < 8 + 2 * c.rtt / 1000: + # the second new line, as daemon.py's made_live: the first may answer a + # check the client took before it sent the key (one check in flight), + # read while another client's view was still the live one + if len(c.check_lines()) > before + 1: + fresh = True + break + time.sleep(0.1) + if not fresh and c.poll() is None: + run.breach(f'{c.label}: freeze: no check within {8 + 2 * c.rtt / 1000:.1f} s at a quiet point') + if fresh: + panes_check(run, c) + if isinstance(c, TtyClient) and fresh: + view_check(run, c) + release(live) + + +def panes_check(run, c): + """The panes on the client's view (/layout, read while its view is the live + one) against the last quiet point's: one still in the session (/index) and no + longer on its view was lost (a rejoin's new view, a view gone wrong).""" + layout = run.nine_read('/layout') or b'' + index = run.nine_read('/index') or b'' + exist = {int(r.split()[0]) for r in index.splitlines() if r.split() and r.split()[0].isdigit()} + shown = set() + for line in layout.decode(errors='replace').splitlines(): + w = line.split() + if len(w) > 6 and w[0].isdigit(): + shown |= {int(x) for x in w[6:] if x.isdigit()} + # Serials both (/layout's pane fields and /index's first are pane serials, + # never reused): a pane closed and another opened in its slot is no loss. + before = getattr(c, 'shown', None) + links_before = getattr(c, 'links_seen', 0) + c.shown = shown + c.links_seen = len(c.links()) + if before is None: + return + lost = (before - shown) & exist + if lost: + c.lost = getattr(c, 'lost', 0) + len(lost) + rows = [r.decode(errors='replace') for r in index.splitlines() if r.split() and r.split()[0].isdigit() and int(r.split()[0]) in lost] + why = 'after a rejoin (its view is new: the known gap)' if c.links_seen > links_before else 'with no rejoin since the last quiet point' + run.breach(f'{c.label}: panes {sorted(lost)} left its view while still open, {why}: {rows}') + + +def view_check(run, c): + """The terminal client's screen and cursor against what the session renders + for the same view (its input was the last, so 9P reads that view).""" + time.sleep(0.4) + raw = run.nine_read('/screen') + if not raw: + return + try: + s = json.loads(raw) + except ValueError: + return + lines, cursor, hidden = c.term.view() + cols, rows = s.get('cols'), s.get('rows') + if (cols, rows) != (c.term.cols, c.term.rows): + c.view_skips = getattr(c, 'view_skips', 0) + 1 + return + theirs = [] + cells = s.get('cells', []) + for r in range(rows): + row = cells[r * cols:(r + 1) * cols] if len(cells) == rows * cols else (cells[r] if r < len(cells) else []) + theirs.append(''.join(cell[0] if isinstance(cell, list) else str(cell) for cell in row).rstrip()) + sc = s.get('cursor') + differ = [r for r in range(min(rows, len(lines))) if lines[r] != theirs[r]] + c.views = getattr(c, 'views', 0) + 1 + want = None + if isinstance(sc, dict): + want = (sc.get('row', sc.get('y')), sc.get('col', sc.get('x'))) + elif isinstance(sc, list) and len(sc) >= 2: + want = (sc[1], sc[0]) + bad_cursor = want is not None and None not in want and tuple(want) != cursor and not hidden + if differ or bad_cursor: + c.view_bad = getattr(c, 'view_bad', 0) + 1 + d = c.dir / f'view-{c.view_bad}.txt' + d.write_text(f'cursor client {cursor} hidden {hidden} session {sc}\nrows differing {differ[:12]}\n' + + ''.join(f'client {r:2} |{lines[r]}|\nsession {r:2} |{theirs[r]}|\n' for r in differ[:12])) + run.breach(f'{c.label}: its screen is not the session\'s render of its view ({len(differ)} rows' + f'{", cursor " + str(cursor) + " vs " + str(want) if bad_cursor else ""}): {d}') + + +def global_event(run, rng, live, made): + """Something happens beside the typing: a resize, a client detached and + another attached, an Upgrade, a script's edit over 9P.""" + kinds = [e for e in run.args.events.split(',') if e] + if run.args.upgrade and not getattr(run, 'upgraded', False): + kinds.append('upgrade') + if not kinds: + return None + what = rng.choice(kinds) + if what == 'resize': + c = rng.choice(live) + rows, cols = rng.randint(18, 50), rng.randint(60, 160) + run.note(f'{c.label} resized to {cols}x{rows}') + c.resize(rows, cols) + elif what == 'detach': + c = rng.choice(live) + run.note(f'{c.label} detached') + hold(run, [c]) + c.asked_to_end = True + c.keys(ESC) + c.keys(b'\x02') + time.sleep(0.2) + c.keys(ESC) + c.keys(b' sD') + try: + wait_for(f'{c.label} let go', lambda: c.poll() is not None, timeout=15) + except TimeoutError: + # Its keys went to a program or a prompt: it is ended as a window closes. + run.note(f'{c.label} did not detach by keys: hung up') + c.hangup() + wait_for(f'{c.label} hung up', lambda: c.poll() is not None, timeout=15) + live.remove(c) + made[0] += 1 + n = start_client(run, c.kind, c.rtt, made[0]) + live.append(n) + return n + elif what == 'upgrade': + run.note('Upgrade') + run.upgraded = True + run.upgrade_times.append(now()) + run.nine_write('/ctl', b'Upgrade\n') + time.sleep(3) + if run.nine: + try: + run.nine.socket.close() + except Exception: + pass + run.nine = None + elif what == 'script': + rows = (run.nine_read('/index') or b'').splitlines() + texts = [r.split()[0].decode() for r in rows if len(r.split()) > 1 and r.split()[1] == b'text'] + if texts: + n = rng.choice(texts) + run.note(f'9P append to pane {n}') + run.nine_write(f'/pane/{n}/body', b'a line a script wrote\n') + return None + + +def soak(run, kinds, rtt, minutes, seed): + """The cell: its minutes of typing, the session started again (and every + client) whenever it dies, as its user would, so a cell measures to its end.""" + rng = random.Random(seed) + end = now() + minutes * 60 + try: + while True: + run.epochs += 1 + died = soak_body(run, kinds, rtt, end, rng) + if not died or now() > end - 30 or run.epochs >= 20 or not run.args.respawn: + break + run.note(f'the session started again (epoch {run.epochs + 1})') + run.reset_session() + except Exception: + import traceback + run.breach('harness: ' + traceback.format_exc().strip().replace('\n', ' | ')[-600:]) + finally: + report_cell(run, rtt) + run.cleanup() + + +def soak_body(run, kinds, rtt, end, rng): + """One session's life in the cell: True if it died before `end`.""" + stop = threading.Event() + live = [] + threads = [] + made = run.made + run.start_session() + for kind in kinds: + made[0] += 1 + live.append(start_client(run, kind, rtt, made[0])) + for c in list(live): + t = threading.Thread(target=monkey, args=(c, random.Random(rng.random()), stop), daemon=True) + t.start() + threads.append(t) + next_quiet = now() + 45 + next_event = now() + rng.uniform(20, 60) + while now() < end: + time.sleep(0.5) + for c in list(live): + if c.poll() is not None: + run.breach(f'{c.label}: exited {c.gone} unasked: {tail(c)}') + live.remove(c) + c.ended_at = now() + if run.session_alive() and run.args.respawn: + # As its user would: open it again, and go on. + made[0] += 1 + try: + n = start_client(run, c.kind, c.rtt, made[0]) + except TimeoutError as e: + run.breach(f'{c.kind}: a client started again gave no first frame: {e}') + continue + live.append(n) + t = threading.Thread(target=monkey, args=(n, random.Random(rng.random()), stop), daemon=True) + t.start() + threads.append(t) + if not run.session_alive(): + run.breach(f'the session died (epoch {run.epochs}, {now() - run.t0:.0f} s into the cell)') + stop.set() + for c in live: + c.asked_to_end = True + for c in live: + try: + wait_for(f'{c.label}\'s end with its session', lambda: c.poll() is not None, timeout=10) + except TimeoutError: + run.breach(f'{c.label}: still running 10 s after its session died') + c.p.kill() + return True + if not live: + break + if now() > next_event: + n = global_event(run, rng, live, made) + if n: + t = threading.Thread(target=monkey, args=(n, random.Random(rng.random()), stop), daemon=True) + t.start() + threads.append(t) + next_event = now() + rng.uniform(20, 70) + if now() > next_quiet: + quiet_point(run, live) + next_quiet = now() + rng.uniform(40, 80) + stop.set() + for t in threads: + t.join(timeout=20) + if run.session_alive() and live: + quiet_point(run, live) + # Each client's links written: a hangup ends a client the normal way. + for c in live: + c.asked_to_end = True + if c.p.poll() is None: + c.hangup() + for c in live: + try: + wait_for(f'{c.label}\'s end', lambda: c.poll() is not None, timeout=20) + except TimeoutError: + run.breach(f'{c.label}: would not end on a hangup') + c.p.kill() + if run.session_alive(): + run.end_session() + return False + + +def tail(c): + if isinstance(c, WinClient): + return c.said()[-300:].replace('\n', ' | ') + lines, _, _ = c.term.view() + return ' | '.join(line for line in lines if line.strip())[-300:] + + +ROWS = [] + + +def cores(run): + """systemd-coredump's cores since the cell began, of the pids it started.""" + if not shutil.which("coredumpctl"): + return [] + since = time.time() - (now() - run.t0) - 5 + got = subprocess.run(['coredumpctl', 'list', '--no-legend', '--no-pager', f'--since=@{since:.0f}'], capture_output=True, text=True) + ours = set(run.pids) | {c.p.pid for c in run.clients if c.p} | ({run.session_pid} if run.session_pid else set()) + out = [] + for line in got.stdout.splitlines(): + w = line.split() + pid = next((int(x) for x in w if x.isdigit() and int(x) in ours), None) + if pid: + out.append(f'pid {pid} {" ".join(x for x in w if x.startswith("SIG"))} {w[-2] if len(w) > 1 else ""}') + return out + + +def report_cell(run, rtt): + for core in cores(run): + run.breach(f'core dumped: {core} (coredumpctl debug <pid>)') + said = run.session_said() + for word in ('panic', 'Segmentation', 'abort', 'reached unreachable', 'core dumped'): + if word in said: + run.breach(f'session said "{word}": {said[said.find(word) - 200:said.find(word) + 300]!r}') + break + if run.crashes(): + run.breach(f'crashes: {run.crashes()[-400:]}') + for c in run.clients: + st = c.stats_sum() + links = len(c.links()) + lines = c.check_lines() + differ = [line for line in lines if line.startswith('differ')] + diverged, upgraded = c.rejoins() + if diverged: + run.breach(f'{c.label}: rejoined {diverged} times after a divergence (links {links}, {upgraded} after an Upgrade)') + mins = ((c.ended_at if hasattr(c, 'ended_at') else now()) - c.started) / 60 + if mins >= 1 and st['visible'] / mins >= 1: + run.breach(f'{c.label}: {st["visible"]} visible rollbacks in {mins:.1f} min (want under 1 a minute)') + if differ: + run.breach(f'{c.label}: {len(differ)} checks differ') + if c.gone is not None and c.gone < 0 and c.gone not in (-signal.SIGHUP, -signal.SIGKILL): + run.breach(f'{c.label}: died of signal {-c.gone}') + if c.gone not in (None, 0) and not c.asked_to_end: + pass # said when found + elif c.gone not in (None, 0, 1, -signal.SIGHUP, -signal.SIGKILL) and c.asked_to_end: + run.breach(f'{c.label}: ended {c.gone} after a detach or hangup: {tail(c)}') + joined = c.said() + for word in ('panic', 'reached unreachable', 'Segmentation', 'General protection', 'aborting'): + if word in joined: + run.breach(f'{c.label} said "{word}": {joined[joined.find(word) - 200:joined.find(word) + 300]!r}') + break + if isinstance(c, WinClient): + lat, missed = c.latencies() + else: + lat, missed = c.lat, c.no_output + dur = max(1e-6, ((c.ended_at if hasattr(c, 'ended_at') else now()) - c.started) / 60) + ROWS.append({ + 'cell': run.cell, 'client': c.label, 'rtt': rtt, 'min': dur, 'inputs': c.inputs, 'links': links, 'rejoins': diverged, + 'checks': len(lines), 'differ': len(differ), 'visible': st['visible'], 'mispred': st['mispredicted'], + 'vis/min': st['visible'] / dur, 'spec/in': f"{st['at_once']}/{st['inputs']}", 'echo': f"{st['echoed']}/{st['typed']}", 'echo_missed': st['missed'], 'clears/min': (c.term.clears / dur) if isinstance(c, TtyClient) else None, + 'p50': pct(lat, 50), 'p99': pct(lat, 99), 'nolat': missed, 'views': getattr(c, 'views', 0), + 'view_bad': getattr(c, 'view_bad', 0), 'lost': getattr(c, 'lost', 0), 'exit': c.gone, 'epochs': run.epochs, + }) + st = (run.root / 'session.stats') + text = '' + if isinstance(run, BoxRun): + text = run.box(f'cat {run.home}/session.stats 2>/dev/null', timeout=30).stdout.decode() + elif st.exists(): + text = st.read_text() + for line in text.splitlines(): + if line.startswith('session inputs'): + w = line.split() + d = {w[i]: w[i + 1] for i in range(1, len(w) - 1, 2)} + worst = int(d.get('boundary_us', '0/0/0').split('/')[2]) + ROWS.append({'cell': run.cell, 'client': 'session', 'rtt': rtt, 'boundary_us': d.get('boundary_us'), + 'input_us': d.get('input_us'), 'cut': d.get('cut')}) + if worst > 250_000: + run.breach(f'session: a boundary took {worst / 1000:.0f} ms') + run.note('end of cell') + + +def table(rows): + cols = ['cell', 'client', 'min', 'inputs', 'links', 'rejoins', 'checks', 'differ', 'spec/in', 'visible', 'mispred', 'vis/min', 'echo', 'echo_missed', 'clears/min', + 'p50', 'p99', 'nolat', 'views', 'view_bad', 'lost', 'exit', 'epochs'] + def fmt(v): + if v is None: + return '-' + if isinstance(v, float): + return f'{v:.1f}' + return str(v) + out = [] + widths = {c: len(c) for c in cols} + body = [] + for r in rows: + if r['client'] == 'session': + body.append(f"{r['cell']:<8} session boundary_us p50/p99/max {r['boundary_us']} input_us {r['input_us']} cut {r['cut']}") + continue + vals = [fmt(r.get(c)) for c in cols] + for c, v in zip(cols, vals): + widths[c] = max(widths[c], len(v)) + body.append(vals) + out.append(' '.join(c.ljust(widths[c]) for c in cols)) + for b in body: + out.append(b if isinstance(b, str) else ' '.join(v.ljust(widths[c]) for c, v in zip(cols, b))) + return '\n'.join(out) + + +# ---- instant: does each kind of local action feel local? ---- +# +# The user: "make sure to use the mosh tricks so that user interaction to +# type/scroll/move the cursor feels instant specially when they don't require +# a request going to the daemon". Per action class and client kind: the share +# of its inputs applied at once (not one of them waiting a round trip), its +# keystroke to present p50/p99 against a local session's measured in the same +# run, and for typing at a shell prompt the share of keys whose echo was +# predicted. A class runs in a link of its own (a client started for it), and +# again with only its setup, so its stats line less the setup's is the class's. + +INSTANT_N = 25 + + +def gen_letters(c, rng, i): + c.keys(bytes([97 + rng.randrange(26)])) + + +def gen_moves(c, rng, i): + c.keys([b'j', b'k', b'w', b'b', b'l', b'h', b'e', b'j'][i % 8]) + + +def gen_scroll_keys(c, rng, i): + c.keys(b'\x04' if i % 2 == 0 else b'\x15') + + +def gen_select(c, rng, i): + c.keys(b'x' if i % 3 else b'w') + + +def gen_term_keys(c, rng, i): + c.keys(b'k' if i % 4 else b'\x15') + + +def gen_click(c, rng, i): + c.click(1, rng.uniform(0.15, 0.9), rng.uniform(0.14, 0.4)) + + +def gen_wheel_file(c, rng, i): + c.mouse('wheel', 1 if i % 3 == 2 else -1, 0.5, 0.3) + + +def gen_wheel_term(c, rng, i): + c.mouse('wheel', 1 if i % 3 != 2 else -1, 0.5, 0.85) + + +# name, pane it is in, setup keys, one measured input, keys after, local only +CLASSES = [ + ('type_shell', 'term', b'', gen_letters, b'\x15', False), + ('type_file', 'file', b'i', gen_letters, ESC, True), + ('move_keys', 'file', b'', gen_moves, b'', True), + ('move_click', 'file', b'', gen_click, b'', True), + ('scroll_keys', 'file', b'', gen_scroll_keys, b'', True), + ('scroll_wheel', 'file', b'', gen_wheel_file, b'', True), + ('term_scroll_keys', 'term', b'\x02', gen_term_keys, b'\x02', True), + ('term_scroll_wheel', 'term', b'', gen_wheel_term, b'', True), + ('select', 'file', b'', gen_select, b';', True), + ('picker', 'file', b' bb', gen_letters, ESC, True), +] + + +def instant_panes(run): + rows = (run.nine_read('/index') or b'').decode().splitlines() + big = next(r.split()[0] for r in rows if r.split()[1] == 'text' and 'big.txt' in r) + term = next(r.split()[0] for r in rows if r.split()[1] == 'term') + return big, term + + +def instant_setup(run, where): + """The pane it starts in: big.txt at line 4000 (normal mode), or the shell + at its prompt with 3000 lines of scrollback above. 9P acts in the view + that typed last: the client's, its join's inputs.""" + big, term = run.instant_panes + run.nine_write('/focus', (big if where == 'file' else term).encode()) + if where == 'file': + run.nine_write(f'/pane/{big}/dot', b'4000') + time.sleep(0.4) + + +def present_ms(c, t0, t1): + """Each measured input's keystroke to present, ms, and how many showed + nothing within 2 s.""" + if isinstance(c, TtyClient): + with c.lock: + return list(c.lat), c.no_output + try: + lines = c.latency.read_text().split('\n') + except FileNotFoundError: + return [], 0 + reads = [int(w[1]) for w in (line.split() for line in lines) if len(w) >= 2 and w[0] == 'R' and t0 * 1e9 <= int(w[1]) <= t1 * 1e9] + frames = sorted(int(w[2]) for w in (line.split() for line in lines) if len(w) >= 3 and w[0] == 'F') + out, missed = [], 0 + for r in reads: + f = next((x for x in frames if x >= r), None) + if f is not None and f - r <= 2e9: + out.append((f - r) / 1e6) + else: + missed += 1 + return out, missed + + +def instant_class(run, c, cls): + """One class in client `c` (or the local session's frontend): its setup, + its N inputs a frame apart or more, its keys after. Its present latencies, + the inputs that showed nothing in 2 s, and the replica's own count of the + class's events that stopped at a host call (its trace: PARDES_REPLICA_TRACE).""" + name, where, setup, gen, after, _ = cls + rng = random.Random(sum(name.encode())) + instant_setup(run, where) + if setup: + c.keys(setup) + time.sleep(0.8) + if isinstance(c, TtyClient): + with c.lock: + c.lat.clear() + c.no_output = 0 + said = len(c.said()) + t0 = time.monotonic() + for i in range(INSTANT_N): + gen(c, rng, i) + time.sleep(0.16 + rng.uniform(0, 0.06)) + t1 = time.monotonic() + time.sleep(2.2) + lat, missed = present_ms(c, t0, t1) + stops = c.said()[said:].count('stopped at a host call') + if after: + c.keys(after) + time.sleep(0.5) + return lat, missed, stops + + +def local_session(run, kind): + """A session of this machine's own, the regular path (not detached, no + replica): the baseline, in a terminal or a window, on big.txt and a shell.""" + name = f'local-{kind}' + e = dict(run.env) + big = run.work / 'big.txt' + if kind == 'tty': + c = TtyClient(run, name, None) + c.p = c.term.spawn([run.pardes, f'--9p={name}', str(big)], e, run.work, open(c.err, 'w')) + else: + c = WinClient(run, name, None) + c.p = subprocess.Popen([run.gui, f'--9p={name}', str(big)], env=dict(e, PARDES_TEST='1', PARDES_TEST_CAPTURE_DIR=str(c.cap), + PARDES_TEST_LATENCY=str(c.latency), SDL_VIDEO_DRIVER='offscreen', PARDES_TEST_COLS=str(c.cols), + PARDES_TEST_ROWS=str(c.rows)), cwd=run.work, stdin=c.slave, stdout=subprocess.DEVNULL, + stderr=open(c.err, 'w'), start_new_session=True) + c.started = now() + run.pids.append(c.p.pid) + run.clients.append(c) + run.nine_sock = run.root / f'pardes-9p-{name}.sock' + run.nine = None + wait_for(f'{name}\'s 9P', run.nine_sock.exists, timeout=30) + wait_for(f'{name}\'s first frame', c.ready, timeout=30) + run.nine_write('/pane/1/exec', b'Tty\n') + time.sleep(1) + run.instant_panes = instant_panes(run) + run.nine_write(f'/pane/{run.instant_panes[1]}/body', b'seq 1 3000\n') + time.sleep(1.5) + return c + + +def instant(args): + kinds = args.clients.split(',') + cells = ['box'] if args.where == 'box' else [int(x) for x in args.rtts.split(',')] + results = [] + runs = [] + base = {} + # The baseline: each class in a local session's own frontend. + run = Run(args, 'instant-local') + runs.append(run) + fixture(run.work, random.Random(args.seed)) + try: + for kind in kinds: + c = local_session(run, kind) + for cls in CLASSES: + lat, missed, _ = instant_class(run, c, cls) + base[(kind, cls[0])] = lat + c.hangup() + time.sleep(1) + if c.p.poll() is None: + c.p.kill() + finally: + run.cleanup() + for cell in cells: + run = BoxRun(args, 'instant-box') if cell == 'box' else Run(args, f'instant{cell}') + rtt = 207 if cell == 'box' else cell + runs.append(run) + run.opens = 'big.txt' + run.shells = 1 + # The replica's own words for each input that waited (the client's + # trace alone; the session keeps its one-word digests). + run.env['PARDES_REPLICA_TRACE'] = '1' + try: + run.start_session() + run.instant_panes = instant_panes(run) + run.nine_write(f'/pane/{run.instant_panes[1]}/body', b'seq 1 3000\n') + time.sleep(2) + for kind in kinds: + c = None + for cls in CLASSES: + if not run.session_alive(): + run.breach(f'{kind} {cls[0]}: the session died') + break + if c is None or c.poll() is not None: + run.made[0] += 1 + c = start_client(run, kind, rtt, run.made[0]) + time.sleep(0.6) + lat, missed, stops = instant_class(run, c, cls) + echo = None + if cls[0] == 'type_shell' and c.poll() is None: + # Its link alone: its echo counts are this class's. + c.asked_to_end = True + c.hangup() + try: + wait_for(f'{c.label}\'s end', lambda: c.poll() is not None, timeout=20) + except TimeoutError: + c.p.kill() + st = c.stats_sum() + echo = (st['echoed'], st['typed']) + c = None + died = c is not None and c.poll() is not None + if died: + run.breach(f'{kind} {cls[0]}: the client died ({c.gone}): {tail(c)[-200:]}') + events = INSTANT_N * (2 if cls[0] == 'move_click' else 1) + # At once: no stop, and shown sooner than a round trip could + # bring it (an input queued behind a stopped one says nothing). + # (Half a round trip, or two frames: a window's software present + # alone takes up to a frame or so.) + quick = sum(1 for x in lat if x < max(rtt / 2, 2000 / 60)) if rtt else len(lat) + frac = min(1 - stops / events, quick / INSTANT_N) + results.append((run.cell, kind, cls, frac, stops, quick, pct(lat, 50), pct(lat, 99), missed, + pct(base.get((kind, cls[0]), []), 99), died, echo)) + if c is not None and c.poll() is None: + c.asked_to_end = True + c.hangup() + try: + wait_for(f'{c.label}\'s end', lambda: c.poll() is not None, timeout=20) + except TimeoutError: + c.p.kill() + except Exception: + import traceback + run.breach('harness: ' + traceback.format_exc().strip().replace('\n', ' | ')[-600:]) + finally: + run.cleanup() + frame = 1000 / 60 + print(f'{"cell":12} {"kind":4} {"class":18} {"at once":>8} {"stops":>5} {"quick":>5} {"p50 ms":>7} {"p99 ms":>7} {"local p99":>9} {"none":>4} {"echo pred":>9}') + for r in results: + cell, kind, cls, frac, stops, quick, p50, p99, missed, lp99, died, echo = r + name, local_only = cls[0], cls[5] + fmt = lambda v: '-' if v is None else f'{v:.1f}' + ep = f'{echo[0]}/{echo[1]}' if echo else '' + print(f'{cell:12} {kind:4} {name:18} {100 * frac:7.0f}% {stops:>5} {quick:>5} {fmt(p50):>7} {fmt(p99):>7} {fmt(lp99):>9} {missed:>4} {ep:>9}') + run = next(x for x in runs if x.cell == cell) + if local_only and frac < 0.99: + run.breach(f'{kind} {name}: {100 * frac:.0f}% applied at once ({stops} stopped, {quick} of {INSTANT_N} shown within half a round trip; want >= 99%)') + if local_only and p99 is not None and lp99 is not None and p99 > lp99 + 2 * frame: + run.breach(f'{kind} {name}: present p99 {p99:.1f} ms against a local session\'s {lp99:.1f} (want within 2 frames)') + if missed > 1: + run.breach(f'{kind} {name}: {missed} of {INSTANT_N} inputs showed nothing within 2 s') + if name == 'type_shell': + if not echo or echo[1] == 0: + run.breach(f'{kind} {name}: no key\'s echo predicted or counted') + elif echo[0] / INSTANT_N < 0.95: + # Of the class's letters (the link's count also has its Ctrl-U, + # which is never predicted). + run.breach(f'{kind} {name}: {echo[0]} of its {INSTANT_N} letters\' echo predicted (want >= 95%)') + breaches = [(x.cell, b) for x in runs for b in x.breaches] + for cell, b in breaches: + print(f'BREACH {cell}: {b}') + for x in runs: + if args.keep or x.breaches: + print(f'kept: {x.root}') + else: + shutil.rmtree(x.root, ignore_errors=True) + print('instant:', 'FAIL' if breaches else 'pass') + sys.exit(1 if breaches else 0) + + +def main(): + ap = argparse.ArgumentParser(description=__doc__.split('\n\n')[0]) + ap.add_argument('where', choices=['local', 'box', 'instant']) + ap.add_argument('--box', action='store_true', help='instant: the session on the box (else local, at each --rtts)') + ap.add_argument('--minutes', type=float, default=None) + ap.add_argument('--rtts', default='0,30,80,207') + ap.add_argument('--jitter', type=float, default=10, help='ms of jitter a chunk, the relay\'s') + ap.add_argument('--seed', type=int, default=1) + ap.add_argument('--clients', default='tty,win', help='kinds, one client each: tty,win,win...') + ap.add_argument('--pardes', default=os.path.expanduser('~/.local/bin/pardes')) + ap.add_argument('--gui', default=os.path.expanduser('~/.local/bin/pardes-gui')) + ap.add_argument('--remote-bin', default=f'{BOX_T}/bin/pardes-hunt') + ap.add_argument('--no-respawn', dest='respawn', action='store_false', help='a client that dies stays dead') + ap.add_argument('--trace', action='store_true', help='PARDES_REPLICA_TRACE on the session: per-field digests') + ap.add_argument('--no-upgrade', dest='upgrade', action='store_false') + ap.add_argument('--near', action='store_true', help='local clients near (no zstd, no blobs by hash)') + ap.add_argument('--keep', action='store_true', help='keep the run\'s files (and the terminals\' output)') + ap.add_argument('--only', default='', help='the monkey\'s actions, by name: move,insert,...') + ap.add_argument('--skip', default='', help='actions left out, by name') + ap.add_argument('--events', default='resize,resize,detach,script,script', help='the global events drawn from') + args = ap.parse_args() + if args.where == 'instant': + args.where = 'box' if args.box else 'local' + return instant(args) + kinds = args.clients.split(',') + runs = [] + if args.where == 'local': + minutes = args.minutes if args.minutes is not None else 2 + for i, rtt in enumerate(int(x) for x in args.rtts.split(',')): + run = Run(args, f'local{rtt}') + run.far = not args.near + runs.append(run) + print(f'soak: {run.cell}: {kinds} for {minutes} min, seed {args.seed + i}, in {run.root}', flush=True) + soak(run, kinds, rtt, minutes, args.seed + i) + else: + minutes = args.minutes if args.minutes is not None else 20 + run = BoxRun(args, 'box') + runs.append(run) + print(f'soak: box: {kinds} for {minutes} min, seed {args.seed}, in {run.root}', flush=True) + soak(run, kinds, 207, minutes, args.seed) + print(table(ROWS)) + breaches = [(r.cell, b) for r in runs for b in r.breaches] + for cell, b in breaches: + print(f'BREACH {cell}: {b}') + for r in runs: + if args.keep or r.breaches: + print(f'kept: {r.root}') + else: + shutil.rmtree(r.root, ignore_errors=True) + print('soak:', 'FAIL' if breaches else 'pass') + sys.exit(1 if breaches else 0) + + +if __name__ == '__main__': + main() diff --git a/test/web-snapshots/README.md b/test/web-snapshots/README.md index a7cd865e..999e8fad 100644 --- a/test/web-snapshots/README.md +++ b/test/web-snapshots/README.md @@ -1,6 +1,6 @@ # Browser interaction tests -`zig build web-snap` loads `touch.dump.zon` and runs `touch.mjs` in Chromium. +`zig build web-snap` loads `touch.dump` (`python3 -B tools/web_fixture.py <pardes> test/web-snapshots/touch.dump` makes it: a native session on `touch-fixture.zig` at 100x30, theme helix, Verbose off) and runs `touch.mjs` in Chromium. The fixed source fixture exercises complete keyword styles, touch Look through the self filesystem, separator resizing, and scrolling without another Look. Assertions locate current pane geometry; no live project source or author paths diff --git a/test/web-snapshots/touch-fixture.zig b/test/web-snapshots/touch-fixture.zig new file mode 100644 index 00000000..b1d5d81e --- /dev/null +++ b/test/web-snapshots/touch-fixture.zig @@ -0,0 +1,58 @@ +pub fn choose(value: u32) []const u8 { + if (value == 42) return "touch fixture"; + return "/virtual/pane/1/body"; +} + +pub const rows = [_]u32{ + 0, + 1, + 2, + 3, + 4, + 5, + 6, + 7, + 8, + 9, + 10, + 11, + 12, + 13, + 14, + 15, + 16, + 17, + 18, + 19, + 20, + 21, + 22, + 23, + 24, + 25, + 26, + 27, + 28, + 29, + 30, + 31, + 32, + 33, + 34, + 35, + 36, + 37, + 38, + 39, + 40, + 41, + 42, + 43, + 44, + 45, + 46, + 47, + 48, + 49, + 50, +};
\ No newline at end of file diff --git a/test/web-snapshots/touch.dump b/test/web-snapshots/touch.dump Binary files differnew file mode 100644 index 00000000..bcc58352 --- /dev/null +++ b/test/web-snapshots/touch.dump diff --git a/test/web-snapshots/touch.dump.zon b/test/web-snapshots/touch.dump.zon deleted file mode 100644 index 230d29e7..00000000 --- a/test/web-snapshots/touch.dump.zon +++ /dev/null @@ -1,73 +0,0 @@ -.{ - .screen = .{ .cols = 100, .rows = 30 }, - .theme = "helix", - .columns = .{.{ .panes = .{0} }}, - .panes = .{.{ - .kind = .file, - .tag = "touch-fixture.zig", - .body = "", - .file = .{ - .path = "touch-fixture.zig", - .content = - \\pub fn choose(value: u32) []const u8 { - \\ if (value == 42) return "touch fixture"; - \\ return "/virtual/pane/1/body"; - \\} - \\ - \\pub const rows = [_]u32{ - \\ 0, - \\ 1, - \\ 2, - \\ 3, - \\ 4, - \\ 5, - \\ 6, - \\ 7, - \\ 8, - \\ 9, - \\ 10, - \\ 11, - \\ 12, - \\ 13, - \\ 14, - \\ 15, - \\ 16, - \\ 17, - \\ 18, - \\ 19, - \\ 20, - \\ 21, - \\ 22, - \\ 23, - \\ 24, - \\ 25, - \\ 26, - \\ 27, - \\ 28, - \\ 29, - \\ 30, - \\ 31, - \\ 32, - \\ 33, - \\ 34, - \\ 35, - \\ 36, - \\ 37, - \\ 38, - \\ 39, - \\ 40, - \\ 41, - \\ 42, - \\ 43, - \\ 44, - \\ 45, - \\ 46, - \\ 47, - \\ 48, - \\ 49, - \\ 50, - \\}; - , - }, - }}, -} diff --git a/test/web-snapshots/touch.mjs b/test/web-snapshots/touch.mjs index 69f8a4a5..6ced7a29 100644 --- a/test/web-snapshots/touch.mjs +++ b/test/web-snapshots/touch.mjs @@ -1,8 +1,9 @@ import assert from "node:assert/strict"; function tags(frame) { - // This fixture has workspace and column bars in its first two rows. - return frame.styleRows.flatMap((cells, y) => y > 1 && /^\s{3}\S/.test(frame.lines[y]) && cells.some((cell) => cell[2] & 128) ? [y] : []); + // This fixture has workspace and column bars in its first two rows; a + // pane's tag text starts after the two-cell marker gutter. + return frame.styleRows.flatMap((cells, y) => y > 1 && /^\s{2}\S/.test(frame.lines[y]) && cells.some((cell) => cell[2] & 128) ? [y] : []); } function pane(frame, name) { @@ -13,14 +14,17 @@ function pane(frame, name) { return { top, bottom, lines: frame.lines.slice(top + 1, bottom) }; } +// helix's keyword and string/number colours (src/themes/helix.zig). +const KW = 0xd776f9, STR = 0xf3cd7a; + function styled(frame) { const source = pane(frame, "touch-fixture.zig"); for (const [line, word, fg, bold] of [ - ["pub fn choose", "fn", 0xcf87e8, false], + ["pub fn choose", "fn", KW, false], ["pub fn choose", "choose", 0xbdbdbd, false], - ["if (value", "if", 0xcf87e8, false], - ["if (value", "42", 0xe3c78a, false], - ["if (value", '"touch fixture"', 0xe3c78a, false], + ["if (value", "if", KW, false], + ["if (value", "42", STR, false], + ["if (value", '"touch fixture"', STR, false], ]) { const row = source.lines.findIndex((text) => text.includes(line)); assert(row >= 0, `missing source line ${line}`); @@ -35,7 +39,7 @@ function styled(frame) { const number = /\b(\d+),/.exec(line); if (!number) continue; for (let x = number.index; x < number.index + number[1].length; x++) { - assert.equal(frame.styleRows[source.top + 1 + row][x][0], 0xe3c78a, `row ${row} number foreground`); + assert.equal(frame.styleRows[source.top + 1 + row][x][0], STR, `row ${row} number foreground`); } } } @@ -79,7 +83,8 @@ export default async function ({ grid, touch, wait, stable, delay, evaluate, mou renderer.render(); result.closedGapBorders=borders(); result.borderChangeInvalidates=renderer.layerRoot.firstChild!==withGap; - const tagValues=[2,2,0,42,1,0,1,1,1,1,0x334455]; + // The 13 fields of a tag layer: a one-line tag, its caret on line 0. + const tagValues=[2,2,0,42,1,0,1,1,1,1,0x334455,1,0]; wasm.pardes_tag_layer_limit=()=>1; wasm.pardes_tag_layer_value=(_,field)=>tagValues[field]; wasm.pardes_tag_layer_cells=()=>1024; @@ -187,14 +192,15 @@ export default async function ({ grid, touch, wait, stable, delay, evaluate, mou assert.deepEqual(reflected.lines.slice(0, sharedRows), original.lines.slice(0, sharedRows), "Look must read the source pane body"); const center = Math.floor(frame.cols / 2); - const separator = Math.max(original.top, reflected.top) - 1; + // No rule between panes: the lower one's grip, its tag's gutter, moves it. + const separator = Math.max(original.top, reflected.top); const distance = original.top < reflected.top ? 4 : -4; - finger = await touch("down", 2, center, separator, null); + finger = await touch("down", 2, 0, separator, null); for (let step = 1; step <= 8; step++) { - finger = await touch("move", 2, center, separator + distance * step / 8, finger); + finger = await touch("move", 2, 0, separator + distance * step / 8, finger); await delay(20); } - await touch("up", 2, center, separator + distance, finger); + await touch("up", 2, 0, separator + distance, finger); await stable(100, 5000); frame = await grid(); assert.equal(tags(frame).length, 2); |
