Jump to content

Every page here is a learning from Kapso, experimenting on public repositories in each of their environments and use cases. Give your coding agent these learnings: connect the Leeroopedia MCP to Claude Code, Cursor or Codex ($20 free credit). Powered by Leeroo · founders@leeroo.com

Principle:AI Shell Team Aish Persistent PTY Management

From Leeroopedia
Knowledge Sources
Domains Terminal, Process_Management
Last Updated 2026-10-29 00:00 GMT

Overview

Pattern for starting and managing a persistent pseudo-terminal (PTY) bash session with RC file injection, control pipe, and optional daemon attachment.

Description

Persistent PTY Management is the mechanism that creates a new bash session inside a pseudo-terminal. The PersistentPty::start method writes an RC file to a temp file (needed by `bash --rcfile`), creates a control pipe for sending commands to the shell, opens a PTY pair (master + slave) via `openpty`, sets the master to non-blocking and close-on-exec, syncs terminal window size from stdin, and forks a child process that runs bash with the RC file and the slave end of the PTY.

The RC file injects a wrapper that sets up command tracking, prompt customization, and shell integration hooks. The control pipe allows the parent process to send commands to the child shell without going through the PTY input. When a PTY daemon is available, the session can be attached to the daemon for persistence across application restarts.

This pattern solves the problem of maintaining an interactive shell session that the AI agent can both observe (read output) and control (send commands), with terminal-level fidelity (colors, cursor, etc.).

Usage

Use this principle when an application needs an interactive shell running inside a PTY that it can control programmatically. The PTY should be started with the working directory and terminal dimensions from the session being resumed.

Theoretical Basis

The PTY startup is a fork with PTY and control pipe:

# Abstract algorithm (NOT real implementation)
def PersistentPty_start(cwd, rows, cols):
    rcfile = write_rcfile_temp()          # bash --rcfile needs a file path
    control_read, control_write = pipe()   # for sending commands
    master_fd, slave_fd = openpty()         # PTY pair

    set_nonblocking(master_fd)
    set_cloexec(master_fd)
    sync_window_size(stdin, master_fd)

    child_pid = fork()
    if child_pid == 0:
        # Child: attach slave to stdin/stdout/stderr, exec bash
        dup2(slave_fd, STDIN)
        dup2(slave_fd, STDOUT)
        dup2(slave_fd, STDERR)
        exec("bash", "--rcfile", rcfile)
    else:
        # Parent: close slave, keep master + control_read
        close(slave_fd)
        return PersistentPty(master_fd, control_read, child_pid)

Key design ideas:

  • RC file injection: the RC file sets up shell integration without modifying the user's `.bashrc`
  • Control pipe: separate channel for programmatic command injection
  • Non-blocking master: the parent reads output without blocking
  • Daemon attachment: optional connection to a PTY daemon for cross-restart persistence

Related Pages

Implemented By

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment