mcpField
H
headless-terminal

headless-terminal

github.com/letta-aiDevelopment15260 installsUpdated 4w ago
Repository

About headless-terminal

A skill for implementing headless terminal interfaces that control shell sessions programmatically. It covers process management, input and output handling, library selection, and edge-case testing.

Key features

  1. 01Review interface methods and behavior
  2. 02Choose pexpect, pty, or subprocess
  3. 03Manage shell process lifecycle
  4. 04Handle input, output, and signals
  5. 05Test timeouts, encoding, and startup configuration

Use cases

  1. 01Implement programmatic control of shell sessions
  2. 02Run commands that require interactive input
  3. 03Capture terminal output and handle cancellation

Install

Copy the command and run it in a terminal.

$npx skills add https://github.com/letta-ai/skills --skill headless-terminal

After installing, type/headless-terminalin Claude Code to confirm it loaded.

Details

From the project README

Headless Terminal Implementation

Overview

This skill guides the implementation of headless terminal interfaces—programmatic wrappers that control shell sessions without a visible terminal UI. These implementations typically involve spawning shell processes, sending input (keystrokes, commands), and capturing output.

Approach

Step 1: Understand the Interface Contract

Before implementing, thoroughly read and understand the interface to be implemented:

  1. Identify all required methods and their signatures
  2. Note return types and expected behaviors
  3. Check for optional methods or features
  4. Look for existing implementations or tests that clarify expected behavior
Step 2: Select the Underlying Library

Common libraries for terminal emulation in Python:

LibraryBest ForTrade-offs
pexpectInteractive terminal emulation, pattern matchingHigher-level API, good for expect/send patterns
ptyLow-level pseudo-terminal controlMore control, but more boilerplate
subprocessSimple non-interactive commandsLimited for true terminal emulation

Decision criteria to document:

  • Does the task require interactive input (passwords, prompts)? → pexpect
  • Does the task need low-level terminal control? → pty
  • Is it purely non-interactive command execution? → subprocess may suffice

Document the choice explicitly with reasoning for future maintainability.

Step 3: Handle Shell Configuration

When spawning a shell process, consider these configuration options:

Interactive mode (-i flag):

  • Ensures startup files (.bashrc, .bash_profile) are sourced
  • Affects signal handling behavior
  • Required if tests verify startup file sourcing

Echo behavior:

  • echo=False in pexpect prevents input from appearing in output
  • Consider whether the use case needs to see echoed input

Terminal dimensions:

  • Default (24, 80) is standard terminal size
  • May affect output formatting for commands that detect terminal width
  • Document why specific dimensions are chosen
Step 4: Implement Core Functionality

Focus on implementing exactly what the interface requires:

  1. Process lifecycle management - Spawn, check liveness, terminate
  2. Input handling - Send keystrokes, handle special keys (Ctrl+C, etc.)
  3. Output capture - Read available output, handle buffering
  4. Resource cleanup - Proper termination, context manager support
Step 5: Address Edge Cases

Explicitly handle these scenarios:

Edge CaseHandling Strategy
Shell exits unexpectedlyCheck process liveness before operations
Long-running commandsConfigurable timeouts, background execution support
Non-ASCII charactersExplicit encoding handling (UTF-8 typically)
Race conditionsAppropriate delays between send and read operations
Command cancellationProper signal handling (SIGINT for Ctrl+C)

Verification Strategies

Testing Approach

Create focused tests for each capability:

  1. Basic functionality - Import, instantiation
  2. Non-interactive commands - Simple command execution and output
  3. Interactive commands - Commands requiring input
  4. Signal handling - Ctrl+C cancellation
  5. Shell state - Variable persistence, environment
  6. Startup configuration - Verify startup files are sourced
  7. Background execution - Long-running command handling
Verification Guidelines
  • Run the test suite to verify all functionality
  • Avoid redundant manual verification after tests pass
  • If a single comprehensive test file exists, use it rather than creating separate verification scripts

Common Pitfalls

Scope Management
  • Implement only what is required - Avoid adding unrequested features like extra utility methods
  • If additional methods seem useful, note them but don't implement unless asked
  • Context manager support is often expected but verify it's in the interface
Efficiency
  • Avoid redundant searches - If searching for a class, one targeted search is sufficient
  • Don't duplicate verification - If tests pass, additional manual checks are unnecessary
  • Don't create example files unless explicitly requested
Documentation Gaps

Common decisions that should be documented but often aren't:

  1. Why the chosen library over alternatives
  2. Why specific shell flags are used
  3. Why specific default values were selected
  4. Thread safety considerations (or explicit statement that it's not thread-safe)
Error Handling

Consider and document handling for:

  • Invalid keystroke sequences
  • Terminal process termination
  • Timeout scenarios
  • Permission issues

Implementation Checklist

Before considering the implementation complete:

  • All interface methods are implemented
  • Library choice is documented with reasoning
  • Configuration decisions are justified
  • Edge cases are explicitly handled or documented as out of scope
  • Tests cover all required functionality
  • No unrequested features were added
  • Resource cleanup is properly implemented

Comments

Markdown supported