@vibelord/lanes 1.6.1 → 1.7.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/payload/lanes/VERSION +1 -1
- package/payload/lanes/bin/lanes +602 -4
- package/payload/lanes/container/lane +185 -5
- package/payload/lanes/container/lane-mcp +319 -55
- package/payload/lanes/container/lane-peerd +704 -0
- package/payload/lanes/container/lane_core.py +2091 -12
- package/payload/lanes/docs/AGENTS-lanes.md +50 -0
- package/payload/lanes/docs/ARCHITECTURE.md +274 -33
- package/payload/lanes/docs/DESIGN.md +7 -3
- package/payload/lanes/docs/RUNBOOK.md +295 -13
- package/payload/lanes/docs/USERGUIDE.md +218 -29
- package/payload/lanes/libexec/agent_state.py +338 -15
- package/payload/lanes/libexec/attach.py +154 -14
- package/payload/lanes/libexec/lanes_lib.py +429 -8
- package/payload/lanes/libexec/sidebar.py +464 -43
- package/payload/lanes/libexec/statusd.py +764 -34
- package/payload/lanes/libexec/tokens.py +14 -0
- package/payload/lanes/systemd/lanes-statusd.service +1 -1
- package/payload/manifest.json +4 -3
|
@@ -1,18 +1,24 @@
|
|
|
1
1
|
#!/usr/bin/env python3
|
|
2
|
-
"""Bound the desktop client's terminal backlog
|
|
2
|
+
"""Bound the desktop client's terminal backlog: shed and repaint a slow display, reconnect only a hung one.
|
|
3
3
|
|
|
4
4
|
Zellij 0.45.1 removes a slow client when its 5,000-message queue fills, but
|
|
5
5
|
sends Exit only after draining that queue. Input can wait for an unblock that
|
|
6
6
|
the removed client will never receive. SSH keepalives cannot detect this.
|
|
7
|
-
This relay
|
|
7
|
+
This relay therefore never stops reading the client. Output the terminal has
|
|
8
|
+
not taken yet is bounded at OUTPUT_LIMIT: beyond it the oldest part is dropped
|
|
9
|
+
and, once the terminal has caught up, the client is asked for a full repaint.
|
|
10
|
+
Only a terminal that accepts no byte at all for OUTPUT_AGE ends the display
|
|
11
|
+
(BEY-6844; the 1.5.1 guard of DEF-1965 also ended slow but healthy displays).
|
|
8
12
|
"""
|
|
9
13
|
import collections
|
|
10
14
|
import errno
|
|
11
15
|
import fcntl
|
|
12
16
|
import os
|
|
13
17
|
import pty
|
|
18
|
+
import re
|
|
14
19
|
import select
|
|
15
20
|
import signal
|
|
21
|
+
import struct
|
|
16
22
|
import sys
|
|
17
23
|
import termios
|
|
18
24
|
import time
|
|
@@ -22,12 +28,47 @@ OUTPUT_LIMIT = 1024 * 1024
|
|
|
22
28
|
OUTPUT_AGE = 5.0
|
|
23
29
|
CHUNK = 65536
|
|
24
30
|
STOP_GRACE = 1.0
|
|
31
|
+
# At most one requested repaint per second: a link that is always too slow gets a repaint a second, not a storm.
|
|
32
|
+
REPAINT_EVERY = 1.0
|
|
33
|
+
# Zellij 0.45.1 repaints on a size it has not seen: one row less, then the real size again. With no gap its client
|
|
34
|
+
# reads the real size twice and nothing is repainted (measured with the pinned binary: 10 ms repaints, 0 does not).
|
|
35
|
+
REPAINT_GAP = 0.1
|
|
36
|
+
# Written where output was dropped: CAN ends an escape sequence the cut left open, and a synchronized update whose
|
|
37
|
+
# end was dropped is ended too. Both are no-ops for a terminal that is not inside either.
|
|
38
|
+
CUT = b'\x18\x1b[?2026l'
|
|
39
|
+
NOTICE = 'lanes: the display fell behind; dropping stale frames and repainting'
|
|
40
|
+
# Operator overrides, read by the `lanes attach` entry below (bin/lanes forwards exactly these two keys).
|
|
41
|
+
BACKLOG_ENV, BACKLOG_RANGE = 'LANES_DISPLAY_BACKLOG_BYTES', (2 * CHUNK, 256 * 1024 * 1024)
|
|
42
|
+
STALL_ENV, STALL_RANGE = 'LANES_DISPLAY_STALL_S', (1.0, 600.0)
|
|
25
43
|
# CAN cancels a partial escape sequence. Leave the alternate screen and restore
|
|
26
44
|
# terminal modes even when the child cannot run its own teardown.
|
|
27
45
|
RESET = (b'\x18\x1b[?2026l\x1b[?1049l\x1b[?1000l\x1b[?1002l\x1b[?1003l'
|
|
28
46
|
b'\x1b[?1006l\x1b[?2004l\x1b[<u\x1b[0m\x1b[?25h')
|
|
29
47
|
|
|
30
48
|
|
|
49
|
+
def settings(environ):
|
|
50
|
+
"""`environ`'s display overrides -> (output limit, stall seconds, [refusal line]). Empty is unset; a value that is
|
|
51
|
+
not a plain number inside its range is refused (named, quoted) and the default stays."""
|
|
52
|
+
limit, age, refused = OUTPUT_LIMIT, OUTPUT_AGE, []
|
|
53
|
+
raw = environ.get(BACKLOG_ENV) or ''
|
|
54
|
+
if raw:
|
|
55
|
+
low, high = BACKLOG_RANGE
|
|
56
|
+
if re.fullmatch(r'[0-9]{1,10}', raw) and low <= int(raw) <= high:
|
|
57
|
+
limit = int(raw)
|
|
58
|
+
else:
|
|
59
|
+
refused.append(f'lanes: {BACKLOG_ENV}={raw[:40]!r} refused (whole bytes from {low} to {high}); '
|
|
60
|
+
f'using {OUTPUT_LIMIT}')
|
|
61
|
+
raw = environ.get(STALL_ENV) or ''
|
|
62
|
+
if raw:
|
|
63
|
+
low, high = STALL_RANGE
|
|
64
|
+
if re.fullmatch(r'[0-9]{1,3}(\.[0-9]{1,3})?', raw) and low <= float(raw) <= high:
|
|
65
|
+
age = float(raw)
|
|
66
|
+
else:
|
|
67
|
+
refused.append(f'lanes: {STALL_ENV}={raw[:40]!r} refused (seconds from {low:g} to {high:g}); '
|
|
68
|
+
f'using {OUTPUT_AGE:g}')
|
|
69
|
+
return limit, age, refused
|
|
70
|
+
|
|
71
|
+
|
|
31
72
|
def relay(command, output_limit=OUTPUT_LIMIT, output_age=OUTPUT_AGE):
|
|
32
73
|
if not command:
|
|
33
74
|
raise ValueError('No display command supplied')
|
|
@@ -36,8 +77,14 @@ def relay(command, output_limit=OUTPUT_LIMIT, output_age=OUTPUT_AGE):
|
|
|
36
77
|
saved = termios.tcgetattr(0)
|
|
37
78
|
size = fcntl.ioctl(0, termios.TIOCGWINSZ, b'\0' * 8)
|
|
38
79
|
flags = fcntl.fcntl(1, fcntl.F_GETFL)
|
|
39
|
-
pending = collections.deque() #
|
|
80
|
+
pending = collections.deque() # bytes the terminal has not taken yet, oldest first; includes partial writes
|
|
40
81
|
pending_size = 0
|
|
82
|
+
last_progress = time.monotonic() # the last write the terminal took (>= 1 byte), or when nothing was pending
|
|
83
|
+
redraw_pending = False # output was dropped since the last repaint request
|
|
84
|
+
last_repaint = None
|
|
85
|
+
restore_at = None # a repaint's second half: when to give the client the real size again
|
|
86
|
+
behind = shed_bytes = 0 # times the display fell behind (shed episodes), and the bytes dropped
|
|
87
|
+
noticed = False
|
|
41
88
|
input_pending = b''
|
|
42
89
|
child = master = None
|
|
43
90
|
status = None
|
|
@@ -58,6 +105,67 @@ def relay(command, output_limit=OUTPUT_LIMIT, output_age=OUTPUT_AGE):
|
|
|
58
105
|
except OSError:
|
|
59
106
|
pass # the terminal may have closed while SIGWINCH was pending
|
|
60
107
|
|
|
108
|
+
def shed(data):
|
|
109
|
+
"""Drop the oldest output until half the limit is left, keep `data`, and mark the cut (BEY-6844). Reading never
|
|
110
|
+
stops, so Zellij's queue for this client never fills."""
|
|
111
|
+
nonlocal pending_size, redraw_pending, behind, shed_bytes
|
|
112
|
+
dropped = 0
|
|
113
|
+
while pending and pending_size > output_limit // 2:
|
|
114
|
+
chunk = pending.popleft() # the oldest, even when the terminal already took a part of it
|
|
115
|
+
pending_size -= len(chunk)
|
|
116
|
+
dropped += len(chunk)
|
|
117
|
+
pending.append(data)
|
|
118
|
+
pending_size += len(data)
|
|
119
|
+
if not dropped: # a limit below two reads: nothing stale to drop, nothing cut
|
|
120
|
+
return
|
|
121
|
+
# Start what is kept at its first escape sequence, not inside one (or inside a UTF-8 character).
|
|
122
|
+
at = pending[0].find(b'\x1b')
|
|
123
|
+
if at > 0:
|
|
124
|
+
pending[0] = pending[0][at:]
|
|
125
|
+
pending_size -= at
|
|
126
|
+
dropped += at
|
|
127
|
+
pending.appendleft(CUT)
|
|
128
|
+
pending_size += len(CUT)
|
|
129
|
+
shed_bytes += dropped
|
|
130
|
+
if not redraw_pending:
|
|
131
|
+
behind += 1
|
|
132
|
+
redraw_pending = True
|
|
133
|
+
|
|
134
|
+
def write_now(data):
|
|
135
|
+
"""Write `data` with nothing pending -> whether the terminal took all of it; the rest is queued in order."""
|
|
136
|
+
nonlocal pending_size, last_progress
|
|
137
|
+
try:
|
|
138
|
+
written = os.write(1, data)
|
|
139
|
+
except BlockingIOError:
|
|
140
|
+
written = 0
|
|
141
|
+
if written:
|
|
142
|
+
last_progress = time.monotonic()
|
|
143
|
+
if written < len(data):
|
|
144
|
+
pending.append(data[written:])
|
|
145
|
+
pending_size += len(data) - written
|
|
146
|
+
return written == len(data)
|
|
147
|
+
|
|
148
|
+
def repaint():
|
|
149
|
+
"""The terminal holds everything kept: cancel what a cut may have left open, then ask for a full frame."""
|
|
150
|
+
nonlocal noticed, restore_at, last_repaint, redraw_pending
|
|
151
|
+
whole = write_now(CUT)
|
|
152
|
+
if whole and not noticed:
|
|
153
|
+
noticed = True
|
|
154
|
+
try: # stderr is this terminal over SSH: written right after CUT, before the repaint replaces it
|
|
155
|
+
os.write(2, f'\r\n{NOTICE}\r\n'.encode())
|
|
156
|
+
except OSError:
|
|
157
|
+
pass
|
|
158
|
+
try:
|
|
159
|
+
rows, cols, xpixel, ypixel = struct.unpack('HHHH', fcntl.ioctl(0, termios.TIOCGWINSZ, b'\0' * 8))
|
|
160
|
+
# A size change makes the kernel send SIGWINCH to the client, as for a real resize.
|
|
161
|
+
fcntl.ioctl(master, termios.TIOCSWINSZ,
|
|
162
|
+
struct.pack('HHHH', rows - 1 if rows > 1 else rows + 1, cols, xpixel, ypixel))
|
|
163
|
+
except OSError:
|
|
164
|
+
pass # the terminal may have closed: the loop ends on its own
|
|
165
|
+
restore_at = time.monotonic() + REPAINT_GAP
|
|
166
|
+
last_repaint = time.monotonic()
|
|
167
|
+
redraw_pending = False
|
|
168
|
+
|
|
61
169
|
signals = (signal.SIGHUP, signal.SIGTERM, signal.SIGINT, signal.SIGWINCH)
|
|
62
170
|
handlers = {sig: signal.getsignal(sig) for sig in signals}
|
|
63
171
|
try:
|
|
@@ -86,26 +194,39 @@ def relay(command, output_limit=OUTPUT_LIMIT, output_age=OUTPUT_AGE):
|
|
|
86
194
|
pid, value = os.waitpid(child, os.WNOHANG)
|
|
87
195
|
if pid:
|
|
88
196
|
status = value
|
|
89
|
-
|
|
197
|
+
now = time.monotonic()
|
|
198
|
+
if not pending:
|
|
199
|
+
last_progress = now
|
|
200
|
+
# Stall rule (BEY-6844): a slow terminal still takes bytes; one that takes none for output_age is hung.
|
|
201
|
+
# Progress, not the age of the oldest output: an age rule also ended slow but healthy displays.
|
|
202
|
+
elif now - last_progress >= output_age:
|
|
90
203
|
reason = 'display output stalled'
|
|
91
204
|
break
|
|
92
205
|
if eof and not pending:
|
|
93
206
|
break
|
|
207
|
+
if restore_at is not None and now >= restore_at:
|
|
208
|
+
restore_at = None
|
|
209
|
+
resize()
|
|
210
|
+
if (redraw_pending and not pending and restore_at is None and not eof and status is None
|
|
211
|
+
and (last_repaint is None or now - last_repaint >= REPAINT_EVERY)):
|
|
212
|
+
repaint()
|
|
94
213
|
readers = ([] if eof else [master]) + ([] if input_pending or status is not None else [0])
|
|
95
214
|
writers = ([1] if pending else []) + ([master] if input_pending and not eof else [])
|
|
96
215
|
readable, writable, _ = select.select(readers, writers, [], 0.05)
|
|
97
216
|
# Drain first: a writable terminal should not trip the byte limit.
|
|
98
217
|
if 1 in writable:
|
|
99
|
-
|
|
218
|
+
data = pending[0]
|
|
100
219
|
try:
|
|
101
220
|
written = os.write(1, data)
|
|
102
221
|
except BlockingIOError:
|
|
103
222
|
written = 0
|
|
223
|
+
if written:
|
|
224
|
+
last_progress = time.monotonic()
|
|
104
225
|
pending_size -= written
|
|
105
226
|
if written == len(data):
|
|
106
227
|
pending.popleft()
|
|
107
228
|
elif written:
|
|
108
|
-
pending[0] =
|
|
229
|
+
pending[0] = data[written:]
|
|
109
230
|
if master in writable:
|
|
110
231
|
try:
|
|
111
232
|
written = os.write(master, input_pending)
|
|
@@ -128,10 +249,9 @@ def relay(command, output_limit=OUTPUT_LIMIT, output_age=OUTPUT_AGE):
|
|
|
128
249
|
if not data:
|
|
129
250
|
eof = True
|
|
130
251
|
elif pending_size + len(data) > output_limit:
|
|
131
|
-
|
|
132
|
-
break
|
|
252
|
+
shed(data) # shed rule (BEY-6844): over the limit drop stale output and repaint, never disconnect
|
|
133
253
|
else:
|
|
134
|
-
pending.append(
|
|
254
|
+
pending.append(data)
|
|
135
255
|
pending_size += len(data)
|
|
136
256
|
finally:
|
|
137
257
|
if child and status is None:
|
|
@@ -157,13 +277,24 @@ def relay(command, output_limit=OUTPUT_LIMIT, output_age=OUTPUT_AGE):
|
|
|
157
277
|
os.close(master)
|
|
158
278
|
# Never block cleanup on the display that caused the timeout. The next
|
|
159
279
|
# attach repaints from current session state; queued stale output dies here.
|
|
160
|
-
ending = RESET
|
|
161
|
-
if reason:
|
|
162
|
-
ending += f'\r\nlanes: {reason}; reconnecting the display.\r\n'.encode()
|
|
163
280
|
try:
|
|
164
|
-
os.write(1,
|
|
281
|
+
os.write(1, RESET)
|
|
165
282
|
except OSError:
|
|
166
283
|
pass
|
|
284
|
+
# The lines go to stderr (this terminal over SSH, after RESET has left the alternate screen). Only a stalled
|
|
285
|
+
# or signalled display reconnects; one that fell behind and caught up says so without "reconnecting".
|
|
286
|
+
lines = []
|
|
287
|
+
if behind:
|
|
288
|
+
times = '1 time' if behind == 1 else f'{behind} times'
|
|
289
|
+
lines.append(f'lanes: the display fell behind {times}; {shed_bytes / 1048576:.1f} MiB of stale frames '
|
|
290
|
+
'were dropped and the screen repainted.')
|
|
291
|
+
if reason:
|
|
292
|
+
lines.append(f'lanes: {reason}; reconnecting the display.')
|
|
293
|
+
if lines:
|
|
294
|
+
try:
|
|
295
|
+
os.write(2, ''.join(f'\r\n{line}' for line in lines).encode() + b'\r\n')
|
|
296
|
+
except OSError:
|
|
297
|
+
pass
|
|
167
298
|
try:
|
|
168
299
|
termios.tcsetattr(0, termios.TCSANOW, saved)
|
|
169
300
|
finally:
|
|
@@ -174,8 +305,17 @@ def relay(command, output_limit=OUTPUT_LIMIT, output_age=OUTPUT_AGE):
|
|
|
174
305
|
return 1 if reason or stopped else (os.waitstatus_to_exitcode(status) if status is not None else 0)
|
|
175
306
|
|
|
176
307
|
|
|
308
|
+
def main(command, environ):
|
|
309
|
+
"""The `lanes attach` entry: the operator's overrides (a refused one is named before the display starts), then
|
|
310
|
+
the relay."""
|
|
311
|
+
output_limit, output_age, refused = settings(environ)
|
|
312
|
+
for line in refused:
|
|
313
|
+
print(line, file=sys.stderr, flush=True)
|
|
314
|
+
return relay(command, output_limit, output_age)
|
|
315
|
+
|
|
316
|
+
|
|
177
317
|
if __name__ == '__main__':
|
|
178
318
|
try:
|
|
179
|
-
sys.exit(
|
|
319
|
+
sys.exit(main(sys.argv[1:], os.environ))
|
|
180
320
|
except (OSError, ValueError) as error:
|
|
181
321
|
sys.exit(f'lanes: display attach failed: {error}')
|