본문으로 건너뛰기
Skillpedia
일반 코딩11k · #11815

hive.terminal-tools-pty-sessions

하는 일

이 스킬은 AI가 여러 명령에 걸쳐 실시간 터미널을 열어 두는 방법을 설명합니다. 저장된 폴더나 설정, 프롬프트 응답, 대화형 도구가 필요한 작업을 위한 것입니다.

사용하는 경우

  • ✓여러 명령이 같은 폴더나 환경 변수를 공유해야 할 때.
  • ✓프로그램이 비밀번호, 호스트 확인 또는 다른 프롬프트를 물어볼 때.
  • ✓Python이나 데이터베이스 프롬프트처럼 대화형 도구를 조작해야 하는 경우.

스킬 링크

https://github.com/aden-hive/hive/blob/main/core/framework/skills/_preset_skills/terminal-tools-pty-sessions/SKILL.md

복사해서 AI(Claude, ChatGPT, Cursor)에 붙여넣고 지침을 따르도록 요청하세요.

사용 방법

가장 쉬운 방법입니다. 메시지를 복사해 AI에 붙여넣으세요. AI가 스킬을 읽고 지침을 따릅니다.

바로 붙여넣을 수 있는 메시지

Use this skill to help me: https://github.com/aden-hive/hive/blob/main/core/framework/skills/_preset_skills/terminal-tools-pty-sessions/SKILL.md Read the SKILL.md file at that link and follow its instructions for my task.
AI가 이런 스킬을 알아서 찾아 쓰길 원하시나요?무료 Skillpedia MCP를 설치하세요. 한 줄이면 작업마다 스킬 15,000+개 중에서 검색합니다.1분 만에 설치 →

AI에게 요청하는 예시

“터미널 세션을 열고 제 프로젝트 폴더로 이동한 다음, 데이터베이스 마이그레이션을 단계별로 실행해 주세요.”

매주 새 스킬 5개. 읽는 데 1분.

작동 방식

AI는 실제 터미널처럼 동작하는 지속형 터미널을 열고, 명령을 보내 출력을 읽습니다. 작업이 끝나면 세션을 닫아서 아무것도 계속 실행되지 않도록 합니다.

기술 내용 (원본 SKILL.md)

작성자의 영어 원문을 GitHub에서 그대로 가져왔습니다.

Persistent PTY sessions

PTY sessions are how you talk to interactive programs — programs that detect a terminal (isatty()) and behave differently when they don't see one. Use a session when:

  • You need state to persist across calls (cd, env vars, sourced scripts)
  • You're driving a REPL (python -i, mysql, psql, node, irb)
  • A program demands an interactive prompt (sudo, ssh, npm login, gh auth login)

For everything else, terminal_exec is simpler. Sessions cost more (per-session bash process, ring buffer, idle-reaping bookkeeping) and have a hard cap (TERMINAL_TOOLS_MAX_PTY, default 8).

Why PTY (and not subprocess pipes)

Subprocess pipes break on every interactive program. The moment a program calls isatty() and sees False, it disables prompts, color, line-editing, password masking, progress bars — sometimes refuses to start. PTY makes us look like a real terminal so these programs work the same as in your shell.

The cost: PTY output includes terminal escape codes (cursor moves, color codes). The session captures them as-is; if you need clean text, strip ANSI escapes in your processing layer.

Bash on macOS — by deliberate policy

terminal_pty_open always invokes /bin/bash, regardless of the user's $SHELL. macOS users: yes, even when zsh is your interactive default. This is the terminal-tools-foundations policy applied to PTYs.

Reasons:

  • zsh has command/builtin classes (zmodload, =cmd expansion, zpty, ztcp) that bypass bash-shaped security checks
  • One shell behavior across platforms eliminates "works on Linux, breaks on macOS" surprises
  • Bash is universal: any shell you've used will accept the bash subset

The bash invocation uses --norc --noprofile so user dotfiles don't leak in. PS1 is set to a unique sentinel for prompt detection. PS2 is empty. PROMPT_COMMAND is empty.

Three modes of terminal_pty_run

1. Default: send command, wait for prompt sentinel

terminal_pty_run(session_id, command="ls -la")
  → { output, prompt_after: True, ... }

The session writes ls -la\n, waits for the sentinel that its custom PS1 emits, returns the slice between submission and prompt. One in-flight call per session — a concurrent call returns a "session busy" error.

2. raw_send: send raw input, no waiting

terminal_pty_run(session_id, command="print('hi')\n", raw_send=True)
  → { bytes_sent: 12 }

For REPLs, vim keystrokes, password prompts. The session writes the bytes and returns immediately — it doesn't wait for a prompt (REPLs don't print bash's prompt; they print their own).

After a raw_send, you typically follow with:

3. read_only: drain currently-buffered output

terminal_pty_run(session_id, read_only=True, timeout_sec=2)
  → { output: "hi\n", more: False, ... }

Reads whatever the session has accumulated since the last drain, with a brief settle window. Use after raw_send to capture the REPL's response.

Custom prompt detection (expect)

When the command launches a program with its own prompt (Python REPL's >>> , mysql's mysql> , sudo's password prompt), the bash sentinel won't appear until the program exits. Override:

terminal_pty_run(session_id, command="python3", expect=r">>>\s*$", timeout_sec=10)
  → output up to and including ">>>", then control returns

For sudo:

terminal_pty_run(session_id, command="sudo -k && sudo whoami", expect=r"[Pp]assword:")
terminal_pty_run(session_id, command="<password>", raw_send=True, command="<password>\n")
terminal_pty_run(session_id, read_only=True, timeout_sec=5)

(Treat passwords carefully — they end up in the ring buffer.)

Always close

terminal_pty_close(session_id)

Leaked sessions count against TERMINAL_TOOLS_MAX_PTY (default 8). Idle reaping happens lazily on every _open call (sessions inactive longer than idle_timeout_sec, default 1800s, are dropped) — but don't rely on it. Close when you're done.

For unresponsive sessions, force=True skips the graceful "exit" attempt and goes straight to SIGTERM/SIGKILL.

Common patterns

Stateful navigation

sid = terminal_pty_open(cwd="/")
terminal_pty_run(sid, command="cd /var/log")
terminal_pty_run(sid, command="ls -la *.log | head")
terminal_pty_close(sid)

Python REPL

sid = terminal_pty_open()
terminal_pty_run(sid, command="python3", expect=r">>>\s*$")
terminal_pty_run(sid, command="x = 42", raw_send=True)
terminal_pty_run(sid, command="print(x*x)\n", r
자주 묻는 질문

명령 한 번 대신 세션을 쓰는 이유는 무엇인가요?

세션은 현재 폴더 같은 상태를 호출 사이에 유지하지만, 단일 명령은 그렇게 하지 못합니다.

작업이 끝나면 세션을 닫아야 하나요?

네. 세션이 계속 열려 자원을 낭비하지 않도록 각 세션을 닫으라고 스킬이 안내합니다.

macOS에서도 작동하나요?

네. macOS에서는 zsh가 기본 셸이어도 세션은 항상 bash를 사용합니다.

상세 정보

작성자의 원본 설명

Use when you need state across calls — building env vars, navigating with cd, driving REPLs (python -i, mysql, psql, node), or responding to interactive prompts (sudo password, ssh host-key confirmation, mysql connection). Teaches the prompt-sentinel exec pattern (default mode), raw I/O for REPLs (raw_send=True then read_only=True), the one-in-flight-per-session rule, and the close-or-leak-against-the-cap discipline. Bash on macOS — never zsh; explicit shell=/bin/zsh is rejected. Read before calling terminal_pty_open.

repo: aden-hive/hive

인기도

GitHub 스타
11k
순위
#11815
라이선스
Apache-2.0
업데이트
2026. 11. 2.

이 스킬이 유용한가요?

0

로그인 투표하려면 로그인하세요.

댓글

  • …

이 저장소의 다른 스킬

비슷한 스킬