improve interactive tty process capture

This commit is contained in:
AI Christianson 2025-02-12 14:58:58 -05:00
parent 7598d42cf9
commit dc079c5d0e
2 changed files with 98 additions and 67 deletions

View File

@ -1,90 +1,121 @@
#!/usr/bin/env python3
""" """
Module for running interactive subprocesses with output capture. Module for running interactive subprocesses with output capture.
It uses a pseudo-tty and integrates pyte's HistoryScreen to
simulate a terminal and capture the final scrollback history (non-blank lines).
The interface remains compatible with external callers expecting a tuple (output, return_code),
where output is a bytes object (UTF-8 encoded).
""" """
import os import os
import re
import shlex import shlex
import shutil import shutil
import tempfile import errno
import subprocess
from typing import List, Tuple from typing import List, Tuple
# Add macOS detection import pyte
IS_MACOS = os.uname().sysname == "Darwin" from pyte.screens import HistoryScreen
def render_line(line, columns: int) -> str:
"""Render a single screen line from the pyte buffer (a mapping of column to Char)."""
return "".join(line[x].data for x in range(columns))
def run_interactive_command(cmd: List[str]) -> Tuple[bytes, int]: def run_interactive_command(cmd: List[str]) -> Tuple[bytes, int]:
""" """
Runs an interactive command with a pseudo-tty, capturing combined output. Runs an interactive command with a pseudo-tty, capturing final scrollback history.
Assumptions and constraints: Assumptions and constraints:
- We are on a Linux system with script available - Running on a Linux system.
- `cmd` is a non-empty list where cmd[0] is the executable - `cmd` is a non-empty list where cmd[0] is the executable.
- The executable and script are assumed to be on PATH - The executable is on PATH.
- If anything is amiss (e.g., command not found), we fail early and cleanly
The output is cleaned to remove ANSI escape sequences and control characters.
Returns: Returns:
Tuple of (cleaned_output, return_code) A tuple of (captured_output, return_code), where captured_output is a UTF-8 encoded
bytes object containing the trimmed non-empty history lines from the terminal session.
Raises:
ValueError: If no command is provided.
FileNotFoundError: If the command is not found in PATH.
RuntimeError: If an error occurs during execution.
""" """
# Fail early if cmd is empty # Fail early if cmd is empty.
if not cmd: if not cmd:
raise ValueError("No command provided.") raise ValueError("No command provided.")
# Check that the command exists # Check that the command exists.
if shutil.which(cmd[0]) is None: if shutil.which(cmd[0]) is None:
raise FileNotFoundError(f"Command '{cmd[0]}' not found in PATH.") raise FileNotFoundError(f"Command '{cmd[0]}' not found in PATH.")
# Create temp files (we'll always clean them up) # Determine terminal dimensions; use os.get_terminal_size if available.
output_file = tempfile.NamedTemporaryFile(prefix="output_", delete=False)
retcode_file = tempfile.NamedTemporaryFile(prefix="retcode_", delete=False)
output_path = output_file.name
retcode_path = retcode_file.name
output_file.close()
retcode_file.close()
# Quote arguments for safety
quoted_cmd = " ".join(shlex.quote(c) for c in cmd)
# Use script to capture output with TTY and save return code
shell_cmd = f"{quoted_cmd}; echo $? > {shlex.quote(retcode_path)}"
def cleanup():
for path in [output_path, retcode_path]:
if os.path.exists(path):
os.remove(path)
try: try:
# Disable pagers by setting environment variables term_size = os.get_terminal_size()
os.environ["GIT_PAGER"] = "" cols, rows = term_size.columns, term_size.lines
os.environ["PAGER"] = "" except OSError:
cols, rows = 80, 24
# Run command with script for TTY and output capture # Instantiate HistoryScreen with a large history (scrollback) buffer.
if IS_MACOS: screen = HistoryScreen(cols, rows, history=1000, ratio=0.5)
os.system(f"script -q {shlex.quote(output_path)} {shell_cmd}") stream = pyte.Stream(screen)
else:
os.system(
f"script -q -c {shlex.quote(shell_cmd)} {shlex.quote(output_path)}"
)
# Read and clean the output # Open a new pseudo-tty.
with open(output_path, "rb") as f: master_fd, slave_fd = os.openpty()
output = f.read()
# Clean ANSI escape sequences and control characters # Spawn the subprocess with its stdio attached to the slave end.
output = re.sub(rb"\x1b\[[0-9;]*[a-zA-Z]", b"", output) # ANSI escape sequences proc = subprocess.Popen(
output = re.sub(rb"[\x00-\x08\x0b\x0c\x0e-\x1f]", b"", output) # Control chars cmd,
stdin=slave_fd,
stdout=slave_fd,
stderr=slave_fd,
close_fds=True
)
os.close(slave_fd) # Close slave in the parent.
# Get the return code # Read output from the master file descriptor in real time.
with open(retcode_path, "r") as f: try:
return_code = int(f.read().strip()) while True:
try:
except Exception as e: data = os.read(master_fd, 1024)
# If something goes wrong, cleanup and re-raise except OSError as e:
cleanup() if e.errno == errno.EIO:
raise RuntimeError("Error running interactive capture") from e # Expected error when the slave side is closed.
break
else:
raise
if not data:
break
# Feed the decoded data into pyte to update the screen and history.
stream.feed(data.decode("utf-8", errors="ignore"))
# Also write the raw data to stdout for live output.
os.write(1, data)
except KeyboardInterrupt:
proc.terminate()
finally: finally:
# Ensure files are removed no matter what os.close(master_fd)
cleanup() proc.wait()
return output, return_code # Assemble full scrollback: combine history.top, the current display, and history.bottom.
top_lines = [render_line(line, cols) for line in screen.history.top]
bottom_lines = [render_line(line, cols) for line in screen.history.bottom]
display_lines = screen.display # List of strings representing the current screen.
all_lines = top_lines + display_lines + bottom_lines
# Trim out empty lines to get only meaningful "history" lines.
trimmed_lines = [line for line in all_lines if line.strip()]
final_output = "\n".join(trimmed_lines)
# Return as bytes for compatibility.
return final_output.encode("utf-8"), proc.returncode
# if __name__ == "__main__":
# # Test command: output 100 lines so that history goes beyond the screen height.
# test_cmd = [
# "bash",
# "-c",
# "for i in $(seq 1 100); do echo \"Line $i\"; sleep 0.05; done"
# ]
# output, ret = run_interactive_command(test_cmd)
# print("\n=== Captured Scrollback (trimmed history lines) ===")
# print(output.decode("utf-8"))
# print("Return code:", ret)

View File

@ -77,8 +77,8 @@ COMMON_TOOLS = get_read_only_tools()
EXPERT_TOOLS = [emit_expert_context, ask_expert] EXPERT_TOOLS = [emit_expert_context, ask_expert]
RESEARCH_TOOLS = [ RESEARCH_TOOLS = [
emit_research_notes, emit_research_notes,
one_shot_completed,
# *TEMPORARILY* disabled to improve tool calling perf. # *TEMPORARILY* disabled to improve tool calling perf.
# one_shot_completed,
# monorepo_detected, # monorepo_detected,
# ui_detected, # ui_detected,
] ]
@ -133,9 +133,9 @@ def get_planning_tools(
# Add planning-specific tools # Add planning-specific tools
planning_tools = [ planning_tools = [
request_task_implementation, request_task_implementation,
plan_implementation_completed,
# *TEMPORARILY* disabled to improve tool calling perf. # *TEMPORARILY* disabled to improve tool calling perf.
# emit_plan, # emit_plan,
# plan_implementation_completed,
] ]
tools.extend(planning_tools) tools.extend(planning_tools)