Repository navigation
Expand file tree
/
Copy pathpty.py
More file actions
349 lines (292 loc) · 12.8 KB
/
Copy pathpty.py
File metadata and controls
349 lines (292 loc) · 12.8 KB
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
"""Pseudo-terminals: ConPTY (pywinpty) on Windows, stdlib pty on Linux/macOS."""
import codecs
import logging
import os
import signal
import subprocess
import sys
import threading
import time
from collections import deque
from pathlib import Path
from . import shells
log = logging.getLogger(__name__)
SCROLLBACK_CHARS = 256 * 1024
# per-session variables of a Claude Code the server was started from (prefixes; user config like
# CLAUDE_CODE_USE_BEDROCK is kept)
PARENT_AGENT_ENV = ("CLAUDECODE", "CLAUDE_PID", "CLAUDE_CODE_CHILD_SESSION", "CLAUDE_CODE_SESSION_", "CLAUDE_CODE_BRIDGE_",
"CLAUDE_CODE_MESSAGING_", "CLAUDE_CODE_ENTRYPOINT")
class Scrollback:
"""Last `limit` chars of output, so a reconnecting client can redraw the screen."""
def __init__(self, limit: int = SCROLLBACK_CHARS):
self.limit = limit
self.chunks: deque[str] = deque()
self.size = 0
self.dirty = False
self.total = 0 # chars ever added, to tell how stale a snapshot is
self.snapshot: tuple[int, str] | None = None # (total at snapshot, the client's terminal text)
self.restored = "" # the previous run's text, shown above the new shell (never part of the live stream)
self.restored_at = ""
def add(self, data: str) -> None:
self.dirty = True
self.total += len(data)
self.chunks.append(data)
self.size += len(data)
while self.size - len(self.chunks[0]) >= self.limit:
self.size -= len(self.chunks.popleft())
if (extra := self.size - self.limit) > 0:
self.chunks[0] = self.chunks[0][extra:]
self.size = self.limit
def text(self) -> str:
return "".join(self.chunks)
def persisted(self) -> str:
"""What to save for a restart: the client's terminal text while it is fresh, else the raw output
(shown as plain text: raw ConPTY output positions the cursor absolutely).
ponytail: staleness = output since the snapshot; a server-side emulator would drop this."""
if self.snapshot and self.total - self.snapshot[0] < 4096:
return self.snapshot[1]
return self.restored + self.text()
# --------------------------------------------------------------- backends
# Both expose: read() -> str (non-blocking), write(str), resize(rows, cols),
# isalive() -> bool, kill().
class WinProc:
def __init__(self, argv: list[str], cwd: str | None, env: dict[str, str], rows: int, cols: int):
import ctypes
from winpty import PTY, Backend
def short(path: str) -> str:
if " " not in path:
return path
buf = ctypes.create_unicode_buffer(512)
n = ctypes.windll.kernel32.GetShortPathNameW(path, buf, 512)
return buf.value if n and buf.value else path
# A detached server (CREATE_NEW_PROCESS_GROUP) ignores Ctrl+C and every shell inherits
# that, so ^C would never interrupt a running command. Re-enable it before spawning.
ctypes.windll.kernel32.SetConsoleCtrlHandler(None, False)
self.pty = PTY(cols, rows, backend=Backend.ConPTY)
cmdline = " " + subprocess.list2cmdline(argv[1:]) if len(argv) > 1 else None
env_str = "\0".join(f"{k}={v}" for k, v in env.items()) + "\0"
self.pty.spawn(short(argv[0]), cwd=cwd, env=env_str, cmdline=cmdline)
self.pid = self.pty.pid
def read(self) -> str:
try:
return self.pty.read(blocking=False)
except Exception: # noqa: BLE001 - winpty raises bare errors at EOF
return ""
def write(self, data: str) -> None:
self.pty.write(data)
def resize(self, rows: int, cols: int) -> None:
self.pty.set_size(cols, rows)
def isalive(self) -> bool:
return self.pty.isalive()
def kill(self) -> None:
pid = self.pty.pid
os.kill(pid, signal.SIGINT)
time.sleep(0.1)
if self.pty.isalive():
os.kill(pid, signal.SIGTERM)
class PosixProc:
def __init__(self, argv: list[str], cwd: str | None, env: dict[str, str], rows: int, cols: int):
import pty
self.master, slave = pty.openpty()
self._winsize(slave, rows, cols)
env.setdefault("TERM", "xterm-256color")
env.setdefault("COLORTERM", "truecolor")
self.proc = subprocess.Popen(
argv,
stdin=slave,
stdout=slave,
stderr=slave,
cwd=cwd,
env=env,
start_new_session=True, # own session + controlling tty
close_fds=True,
)
os.close(slave)
self.pid = self.proc.pid
os.set_blocking(self.master, False)
self.decoder = codecs.getincrementaldecoder("utf-8")("replace")
self.eof = False
@staticmethod
def _winsize(fd: int, rows: int, cols: int) -> None:
import fcntl
import struct
import termios
fcntl.ioctl(fd, termios.TIOCSWINSZ, struct.pack("HHHH", rows, cols, 0, 0))
def read(self) -> str:
if self.eof:
return ""
try:
data = os.read(self.master, 65536)
except BlockingIOError:
return ""
except OSError: # EIO: slave side closed
data = b""
if not data:
self.eof = True
return self.decoder.decode(data, final=not data)
def write(self, data: str) -> None:
os.write(self.master, data.encode())
def resize(self, rows: int, cols: int) -> None:
self._winsize(self.master, rows, cols)
def isalive(self) -> bool:
return not self.eof and self.proc.poll() is None
def kill(self) -> None:
try:
os.killpg(self.proc.pid, signal.SIGHUP)
except OSError:
pass
try:
self.proc.wait(timeout=0.5)
except subprocess.TimeoutExpired:
os.killpg(self.proc.pid, signal.SIGKILL)
os.close(self.master)
Proc = WinProc if sys.platform == "win32" else PosixProc
# ---------------------------------------------------------------- manager
class PtyManager:
def __init__(self, store: Path | None = None):
self.procs: dict[str, Proc] = {}
self.scrollback: dict[str, Scrollback] = {}
self.wakes: dict[str, threading.Event] = {}
self.store = store # scrollback files, so a restart/reboot keeps terminal history
def _file(self, session_id: str) -> Path | None:
if not self.store or not session_id.replace("-", "").isalnum():
return None
return self.store / f"{session_id}.log"
def _restore(self, session_id: str) -> Scrollback:
sb = Scrollback()
f = self._file(session_id)
if f and f.exists() and (old := f.read_bytes().decode("utf-8", errors="replace").rstrip()): # bytes: keep CRLF
sb.restored = old + "\r\n"
sb.restored_at = time.strftime("%Y-%m-%d %H:%M", time.localtime(f.stat().st_mtime))
return sb
def save(self) -> None:
"""Write changed scrollbacks to disk."""
if not self.store:
return
self.store.mkdir(parents=True, exist_ok=True)
for sid, sb in list(self.scrollback.items()):
if sb.dirty and (f := self._file(sid)):
sb.dirty = False
f.write_bytes(sb.persisted().encode("utf-8"))
def snapshot(self, session_id: str, data: str) -> None:
"""Store the client's terminal text (scrollback and screen, plain lines) for the next restart."""
if sb := self.scrollback.get(session_id):
sb.snapshot = (sb.total, data[-SCROLLBACK_CHARS:])
sb.dirty = True
def searchable(self, session_id: str) -> str:
"""Terminal output as saved for restore: from memory while running, else from disk."""
if sb := self.scrollback.get(session_id):
return sb.persisted()
f = self._file(session_id)
return f.read_bytes().decode("utf-8", errors="replace") if f and f.exists() else ""
def forget(self, session_id: str) -> None:
if f := self._file(session_id):
f.unlink(missing_ok=True)
def prune(self, keep: set[str]) -> None:
"""Delete saved scrollback for sessions that no longer exist."""
if self.store and self.store.exists():
for f in self.store.glob("*.log"):
if f.stem not in keep:
f.unlink(missing_ok=True)
def create(
self,
session_id: str,
cwd: str | None = None,
shell: str | None = None,
rows: int = 24,
cols: int = 120,
distro: str | None = None,
extra_env: dict[str, str] | None = None,
) -> Proc:
proc = self.get(session_id)
if proc:
return proc
start_dir = cwd if (cwd and Path(cwd).is_dir()) else None
argv, extra = shells.with_integration(shells.interactive_argv(shell or "", distro, start_dir))
# a server started from inside Claude Code must not pass its session on: a claude run in the
# terminal would think it is a subagent (no transcript, so no context meter) and share its socket
env = {k: v for k, v in os.environ.items() if not k.startswith(PARENT_AGENT_ENV)} | extra
env["SHELLDECK_SESSION_ID"] = session_id
# xterm.js is a modern terminal; without this TUIs that see ConPTY assume conhost (Devin warns)
env["TERM_PROGRAM"] = "shelldeck"
env.update(extra_env or {})
proc = Proc(argv, start_dir, env, rows, cols)
self.procs[session_id] = proc
self.scrollback[session_id] = self._restore(session_id)
log.info("spawned %s for session %s in %s", argv, session_id, start_dir)
return proc
def get(self, session_id: str) -> Proc | None:
proc = self.procs.get(session_id)
if proc and not proc.isalive():
return None
return proc
def history(self, session_id: str) -> str:
sb = self.scrollback.get(session_id)
return sb.text() if sb else ""
def stream(self, session_id: str, emit) -> None:
"""Read the session's output on its own thread, calling emit(str) per chunk and emit(None) at exit.
The thread polls non-blocking reads: pywinpty's blocking read holds data back and never
returns at EOF. After output or a keystroke (write() wakes it) it polls with time.sleep,
which is ~1ms on Windows; asyncio sleeps and Event.wait round up to ~16ms there. Once idle
it waits on the event instead, so a quiet shell costs almost no CPU."""
proc = self.procs.get(session_id)
wake = self.wakes.setdefault(session_id, threading.Event())
def run() -> None:
idle = 0
while proc is not None:
try:
data = proc.read()
except OSError: # POSIX fd closed by kill()
break
if data:
idle = 0
emit(data)
continue
if not proc.isalive():
break
idle += 1
if idle <= 50: # just active or typed into: ~1ms polls for ~75ms
time.sleep(0.001)
elif wake.wait(0.02): # idle: Event.wait is ~16ms-grained on Windows, fine here
wake.clear()
idle = 0
emit(None)
threading.Thread(target=run, name=f"pty-{session_id}", daemon=True).start()
def record(self, session_id: str, data: str) -> None:
if sb := self.scrollback.get(session_id):
sb.add(data)
def write(self, session_id: str, data: str) -> bool:
proc = self.get(session_id)
if not proc:
return False
try:
proc.write(data)
if wake := self.wakes.get(session_id):
wake.set()
return True
except Exception: # noqa: BLE001
log.warning("write failed for session %s", session_id, exc_info=True)
return False
def resize(self, session_id: str, rows: int, cols: int) -> bool:
proc = self.get(session_id)
if not proc:
return False
try:
proc.resize(rows, cols)
return True
except Exception: # noqa: BLE001
log.warning("resize failed for session %s", session_id, exc_info=True)
return False
def list_alive(self) -> list[str]:
return [sid for sid in list(self.procs) if self.get(sid)]
def terminate(self, session_id: str) -> None:
self.scrollback.pop(session_id, None)
if wake := self.wakes.pop(session_id, None):
wake.set()
proc = self.procs.pop(session_id, None)
if not proc:
return
try:
proc.kill() # also releases the pty fd on POSIX, even if the shell already exited
except OSError:
log.warning("terminate failed for session %s", session_id, exc_info=True)