Attach to existing tmux Claude session
This commit is contained in:
@@ -1,8 +1,9 @@
|
||||
"""pexpect 기반 PTY 프로세스 관리."""
|
||||
"""기존 tmux 세션에 attach하여 CLI 입출력을 중계한다."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import logging
|
||||
import subprocess
|
||||
|
||||
import pexpect
|
||||
|
||||
@@ -10,21 +11,37 @@ logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class PtyManager:
|
||||
"""가상 터미널에서 CLI 프로세스를 생성하고 입출력을 제어한다."""
|
||||
"""기존 tmux 세션에 attach하고 입출력을 제어한다."""
|
||||
|
||||
def __init__(self, command: str = "claude") -> None:
|
||||
self.command = command
|
||||
def __init__(self, session_name: str = "claude") -> None:
|
||||
self.session_name = session_name
|
||||
self._process: pexpect.spawn | None = None
|
||||
|
||||
@property
|
||||
def is_alive(self) -> bool:
|
||||
return self._process is not None and self._process.isalive()
|
||||
|
||||
def _ensure_session_exists(self) -> None:
|
||||
"""attach 대상 tmux 세션 존재 여부를 검증한다."""
|
||||
result = subprocess.run(
|
||||
["tmux", "has-session", "-t", self.session_name],
|
||||
check=False,
|
||||
capture_output=True,
|
||||
text=True,
|
||||
)
|
||||
if result.returncode != 0:
|
||||
raise RuntimeError(
|
||||
"tmux 세션이 없습니다. 먼저 실행하세요: "
|
||||
f"tmux new -s {self.session_name} claude"
|
||||
)
|
||||
|
||||
def start(self) -> None:
|
||||
"""프로세스를 시작한다."""
|
||||
logger.info("프로세스 시작: %s", self.command)
|
||||
"""기존 tmux 세션에 attach한다."""
|
||||
self._ensure_session_exists()
|
||||
logger.info("tmux 세션 attach: %s", self.session_name)
|
||||
self._process = pexpect.spawn(
|
||||
self.command,
|
||||
"tmux",
|
||||
["attach-session", "-t", self.session_name],
|
||||
encoding="utf-8",
|
||||
timeout=None,
|
||||
)
|
||||
@@ -49,8 +66,8 @@ class PtyManager:
|
||||
return self._process.before or ""
|
||||
|
||||
def stop(self) -> None:
|
||||
"""프로세스를 종료한다."""
|
||||
"""attach 연결만 종료한다(원격 세션은 유지)."""
|
||||
if self._process is not None:
|
||||
logger.info("프로세스 종료")
|
||||
logger.info("tmux attach 종료")
|
||||
self._process.close(force=True)
|
||||
self._process = None
|
||||
|
||||
Reference in New Issue
Block a user