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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
|
#!/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 = 17
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)
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 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): the next
# unit's digest finds it, it says so in a line and joins again, and
# its keys after go in 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='1'))
started.append(d.pid)
wait_for('client D\'s first frame', lambda: b'line 3 of the notes' in td.text(), timeout=20)
os.write(td.master, b'jj')
time.sleep(.5)
# Another's unit, replayed on D's core: its digest finds it.
os.write(ta.master, b'jj')
wait_for('client D diverged and joined again', lambda: b'joined again' in td.text(), timeout=20)
assert d.poll() is None
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/{pane}/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 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)
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())
refusing('1')
client.write(f'/pane/{shell}/exec', b'Upgrade\n')
client.socket.close()
client = saying(sock, 'it goes on as')
wait_for('the client joined again', lambda: b'joined again: the session is upgrading' in term4.text(), timeout=30)
assert again.poll() is None
seen = len(checks(root / 'e.check'))
os.write(term4.master, b'gg')
wait_for('the client agrees after it joined again', lambda: agreed(root / 'e.check', seen), timeout=20)
again.send_signal(signal.SIGHUP)
again.wait(timeout=10)
term4.close()
print('upgrade: a replica client of the build running joined again after the upgrade, said so and agreed')
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)
|