"""One human command: no TTY, no kernel, bounded sanitized packet output. The BEAM port owns our stdin. EOF ends the entire command group, including on an unclean daemon death. One acknowledged frame at a time bounds pipe/mailboxes. """ from __future__ import annotations import asyncio import codecs import json import os from pathlib import Path import pwd import re import select import struct import subprocess import sys import threading import albedo_proc CAP = 16 * 1024 * 1024 # Each frame waits for the daemon to store it. Output read while one is # stored waits for the next, up to this much; past it, reading stops until # the stored frame is acknowledged. FRAME = 1024 * 1024 def read_exact(size: int) -> bytes: parts = bytearray() while len(parts) < size: chunk = os.read(0, size - len(parts)) if not chunk: break parts.extend(chunk) return bytes(parts) def packet() -> bytes | None: header = read_exact(4) if not header: return None if len(header) != 4: raise EOFError("incomplete shell control frame") size = struct.unpack(">I", header)[0] if size > 262144: raise ValueError("oversized shell control frame") data = read_exact(size) if len(data) != size: raise EOFError("incomplete shell control frame") return data class Sanitizer: """Incremental UTF-8 and ECMA-48 stripping, with constant parser state. Text between control characters is copied in one slice, and a sequence is skipped to its terminator by one search: a per-character loop cost about 2 s of CPU for a 16 MiB capture. """ PLAIN = re.compile(r"[^\x00-\x08\x0b-\x1f\x7f-\x9f]+") CSI_END = re.compile(r"[\x40-\x7e]") STRING_END = re.compile(r"[\x07\x9c\x1b]") def __init__(self) -> None: self.decoder = codecs.getincrementaldecoder("utf-8")("replace") self.state = "text" def feed(self, data: bytes, final: bool = False) -> str: text = self.decoder.decode(data, final) kept = [] at, end = 0, len(text) while at < end: if self.state == "text": plain = self.PLAIN.match(text, at) if plain: kept.append(plain.group()) at = plain.end() continue char = text[at] n = ord(char) if char == "\x1b": self.state = "esc" elif n == 0x9B: self.state = "csi" elif n in (0x90, 0x98, 0x9D, 0x9E, 0x9F): self.state = "string" elif self.state == "esc": char = text[at] self.state = ( "csi" if char == "[" else "string" if char in "]PX^_" else "text" ) elif self.state == "csi": found = self.CSI_END.search(text, at) if not found: break at = found.start() self.state = "text" elif self.state == "string": found = self.STRING_END.search(text, at) if not found: break at = found.start() self.state = "string_esc" if text[at] == "\x1b" else "text" else: self.state = "text" if text[at] == "\\" else "string" at += 1 return "".join(kept) def shell_args(command: str) -> tuple[list[str], str]: shell = os.environ.get("SHELL") if not shell: try: shell = pwd.getpwuid(os.getuid()).pw_shell except KeyError: shell = None shell = shell or "/bin/sh" name = Path(shell).name if name in ("csh", "tcsh"): return ["-" + name, "-c", command], shell if name in ("nu", "nushell"): return [shell, "--login", "-c", command], shell return [shell, "-l", "-c", command], shell def main() -> None: initial = packet() if initial is None: return config = json.loads(initial) ack = threading.Event() disconnected = threading.Event() cancelled = threading.Event() lock = threading.Lock() child: subprocess.Popen[bytes] | None = None group: albedo_proc.Group | None = None cleanup_error: str | None = None def end_group() -> None: nonlocal group, cleanup_error with lock: if group is not None: ending = asyncio.run(albedo_proc.terminate([group]))[0] if ending.gone: group = None else: cleanup_error = ending.report() def controls() -> None: try: while (data := packet()) is not None: if data == b"stop": cancelled.set() end_group() elif data == b"ack": acknowledged() except (OSError, EOFError, ValueError): pass disconnected.set() cancelled.set() acknowledged() end_group() def acknowledged() -> None: ack.set() try: os.write(wake_write, b"\0") except BlockingIOError: pass def send(value: dict[str, object]) -> None: ack.wait() if disconnected.is_set(): raise BrokenPipeError ack.clear() data = json.dumps(value, ensure_ascii=False).encode() sys.stdout.buffer.write(struct.pack(">I", len(data)) + data) sys.stdout.buffer.flush() def emit(value: dict[str, object]) -> None: send(value) ack.wait() if disconnected.is_set(): raise BrokenPipeError # No frame is in flight until the first one is sent. The wake pipe lets # the read loop send waiting output as soon as the previous frame is acked. ack.set() wake_read, wake_write = os.pipe() os.set_blocking(wake_read, False) os.set_blocking(wake_write, False) # Start the child before the control reader so an immediate EOF cannot race # group publication and leave a just-started child without cleanup. try: args, executable = shell_args(config["command"]) child = subprocess.Popen( args, executable=executable, cwd=config["cwd"], stdin=subprocess.DEVNULL, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, start_new_session=True, ) group = albedo_proc.Group(child.pid, albedo_proc.leader_token(child.pid)) threading.Thread(target=controls, daemon=True).start() assert child.stdout is not None fd = child.stdout.fileno() os.set_blocking(fd, False) sanitizer = Sanitizer() captured = 0 truncated = False pending: list[str] = [] pending_bytes = 0 dirty = False def drain() -> tuple[bytes, bool]: """What the pipe holds now, up to the room left in the next frame, and whether output ended.""" data = bytearray() while len(data) < max(1, FRAME - pending_bytes): try: chunk = os.read(fd, max(1, FRAME - pending_bytes) - len(data)) except BlockingIOError: return bytes(data), False if not chunk: return bytes(data), True data.extend(chunk) return bytes(data), False def capture(data: bytes, final: bool = False) -> None: nonlocal captured, truncated, pending_bytes, dirty text = sanitizer.feed(data, final) encoded = text.encode() kept = ( "" if truncated else encoded[: max(0, CAP - captured)].decode("utf-8", "ignore") ) size = len(kept.encode()) dropped = size < len(encoded) dirty = dirty or bool(kept) or (dropped and not truncated) truncated = truncated or dropped captured += size if kept: pending.append(kept) pending_bytes += size def flush(wait: bool) -> None: """Send waiting output when no frame is in flight, or once it is acknowledged if waiting.""" nonlocal pending_bytes, dirty if dirty and (wait or ack.is_set()): send( {"type": "output", "text": "".join(pending), "truncated": truncated} ) pending.clear() pending_bytes = 0 dirty = False output_eof = False while True: if cleanup_error is not None: raise RuntimeError(cleanup_error) flush(pending_bytes >= FRAME) if child.poll() is not None: # A shell may exit while a background descendant holds stdout. # End all descendants before bounded final draining. end_group() if cleanup_error is not None: raise RuntimeError(cleanup_error) while True: data, ended = drain() if data: capture(data) flush(pending_bytes >= FRAME) if ended or not data: break break watched = [wake_read] if output_eof else [fd, wake_read] ready, _, _ = select.select(watched, [], [], 0.1) if wake_read in ready: os.read(wake_read, 4096) if fd in ready: data, ended = drain() if data: capture(data) # EOF does not mean the process stopped: it can close both # output descriptors and keep running. Keep control alive. output_eof = ended capture(b"", final=True) flush(True) end_group() if cleanup_error is not None: raise RuntimeError(cleanup_error) code = child.wait() emit( { "type": "ended", "status": "cancelled" if cancelled.is_set() else "completed", "exit_code": code, "error": None, } ) except BrokenPipeError: pass except Exception as error: threading.Thread( target=controls, daemon=True ).start() if child is None else None try: emit( { "type": "ended", "status": "failed", "exit_code": None, "error": Sanitizer() .feed(str(error).encode(), final=True) .encode()[:4096] .decode("utf-8", "ignore"), } ) except BrokenPipeError: pass finally: end_group() if child is not None: try: child.wait(timeout=3) except subprocess.TimeoutExpired: # The checked cleanup failure is explicit in the terminal frame; # never keep a supervisor stuck forever on an unsignalable child. pass if child.stdout is not None: child.stdout.close() if __name__ == "__main__": main()