Compare commits
17 Commits
feat/remot
...
fix/slack-
| Author | SHA1 | Date | |
|---|---|---|---|
| 6747ab703a | |||
| cfbe781210 | |||
| c2fe922105 | |||
| 0a49032f29 | |||
| cf1a6cb620 | |||
| 909ef08e37 | |||
| 2fddb1ca24 | |||
| 537839ba1a | |||
| 96188680fb | |||
| 6a29c5f3dd | |||
| 7d3540483b | |||
| fb7c25ec78 | |||
| b83929fac2 | |||
| e616bebdb9 | |||
| 8f8e63f56f | |||
| df377cf2df | |||
| 906022555a |
@@ -6,8 +6,9 @@ SLACK_APP_TOKEN=xapp-your-app-token
|
|||||||
SLACK_ALLOWED_USER_ID=U0XXXXXXXXX
|
SLACK_ALLOWED_USER_ID=U0XXXXXXXXX
|
||||||
SLACK_ALLOWED_CHANNEL_ID=C0XXXXXXXXX
|
SLACK_ALLOWED_CHANNEL_ID=C0XXXXXXXXX
|
||||||
|
|
||||||
# PTY 설정
|
# PTY / tmux 연결 설정
|
||||||
DEFAULT_SHELL=claude
|
TMUX_SESSION_NAME=claude
|
||||||
|
CODEX_TMUX_SESSION_NAME=codex
|
||||||
PTY_READ_TIMEOUT=5
|
PTY_READ_TIMEOUT=5
|
||||||
|
|
||||||
# 출력 버퍼 설정
|
# 출력 버퍼 설정
|
||||||
|
|||||||
39
AGENTS.md
39
AGENTS.md
@@ -42,3 +42,42 @@ Current git history is minimal (`Initial commit`), so enforce clear conventions
|
|||||||
## Security & Configuration Tips
|
## Security & Configuration Tips
|
||||||
Do not commit secrets. Copy `.env.example` to `.env` locally.
|
Do not commit secrets. Copy `.env.example` to `.env` locally.
|
||||||
Restrict Slack access with `SLACK_ALLOWED_USER_ID` and `SLACK_ALLOWED_CHANNEL_ID` before running in shared workspaces.
|
Restrict Slack access with `SLACK_ALLOWED_USER_ID` and `SLACK_ALLOWED_CHANNEL_ID` before running in shared workspaces.
|
||||||
|
|
||||||
|
## Git & Gitea Workflow Notes
|
||||||
|
Use `tea` (Gitea CLI), not `gh`.
|
||||||
|
|
||||||
|
Hard rule:
|
||||||
|
- Never implement changes, stage files, or commit on `main`.
|
||||||
|
- Always create/use a feature branch first, and merge via PR.
|
||||||
|
|
||||||
|
- Check login:
|
||||||
|
- `tea login ls`
|
||||||
|
- `tea whoami`
|
||||||
|
- Create/update feature branch:
|
||||||
|
- `git checkout -b <branch>` (or `git checkout <branch>`)
|
||||||
|
- `git add <files>`
|
||||||
|
- `git commit -m "<message>"`
|
||||||
|
- Push to Gitea:
|
||||||
|
- Normal: `git push -u origin <branch>`
|
||||||
|
- If HTTP username prompt fails in this environment, use the token from `~/.config/tea/config.yml`:
|
||||||
|
- `TOKEN=$(sed -n 's/^[[:space:]]*token: //p' ~/.config/tea/config.yml | head -n1)`
|
||||||
|
- `git push "http://agentson:${TOKEN}@localhost:3000/jihoson/LazyEnter.git" <branch>`
|
||||||
|
- After token-based push, ensure tracking is on `origin/<branch>` (not token URL):
|
||||||
|
- `git fetch origin <branch>:refs/remotes/origin/<branch>`
|
||||||
|
- `git branch --set-upstream-to=origin/<branch> <branch>`
|
||||||
|
- Create PR on Gitea:
|
||||||
|
- `tea pr create --base main --head <branch> --title "<title>" --description "<body>"`
|
||||||
|
- If PR body includes backticks (`` ` ``), slash commands, or markdown that can be shell-expanded, do not pass it directly in double quotes.
|
||||||
|
- Preferred safe flow:
|
||||||
|
- `cat > /tmp/pr_body.md <<'EOF'` ... `EOF`
|
||||||
|
- `tea pr create --base main --head <branch> --title "<title>" --description "$(cat /tmp/pr_body.md)"`
|
||||||
|
- If the body is malformed after creation, patch it with API:
|
||||||
|
- `tea api -X PATCH repos/{owner}/{repo}/pulls/<number> -F body=@/tmp/pr_body.md`
|
||||||
|
- Sync local main:
|
||||||
|
- `git checkout main`
|
||||||
|
- `git pull --ff-only`
|
||||||
|
- When user confirms the PR is merged, always run this sync immediately to keep local `main` up to date before any next task.
|
||||||
|
|
||||||
|
Safety:
|
||||||
|
- Do not commit or print tokens in logs/docs.
|
||||||
|
- Keep unrelated local files (for example `uv.lock`) out of PRs unless intentionally changed.
|
||||||
|
|||||||
36
README.md
36
README.md
@@ -1,15 +1,18 @@
|
|||||||
# LazyEnter
|
# LazyEnter
|
||||||
|
|
||||||
소파에서 엔터나 치자.
|
소파에서 엔터나 치자.
|
||||||
로컬 PC의 Claude Code CLI 세션을 Slack Socket Mode로 중계해 모바일에서도 원격으로 입력/승인을 처리하는 브릿지입니다.
|
로컬 PC의 Claude/Codex CLI 세션을 Slack Socket Mode로 중계해 모바일에서도 원격으로 입력/승인을 처리하는 브릿지입니다.
|
||||||
|
|
||||||
## 동작 방식
|
## 동작 방식
|
||||||
|
|
||||||
1. Slack에서 `/start-claude` 실행
|
1. 로컬에서 Claude 또는 Codex를 tmux 세션으로 미리 실행
|
||||||
2. 로컬에서 `claude` 프로세스(기본값)가 PTY로 시작됨
|
- Claude: `tmux new -s claude claude`
|
||||||
3. Slack 채널 메시지가 CLI 입력으로 전달됨
|
- Codex: `tmux new -s codex codex`
|
||||||
4. CLI 출력이 Slack으로 다시 전송됨
|
2. Slack에서 `/start-claude` 또는 `/start-codex` 실행
|
||||||
5. `/stop-claude`로 세션 종료
|
3. 브릿지가 기존 tmux 세션에 attach
|
||||||
|
4. Slack 채널 메시지가 CLI 입력으로 전달됨
|
||||||
|
5. CLI 출력이 Slack으로 다시 전송됨
|
||||||
|
6. `/stop-claude`로 브릿지 연결 해제 (tmux 세션은 유지)
|
||||||
|
|
||||||
## 빠른 시작
|
## 빠른 시작
|
||||||
|
|
||||||
@@ -34,7 +37,8 @@ cp .env.example .env
|
|||||||
- `SLACK_ALLOWED_CHANNEL_ID`
|
- `SLACK_ALLOWED_CHANNEL_ID`
|
||||||
|
|
||||||
선택 환경 변수:
|
선택 환경 변수:
|
||||||
- `DEFAULT_SHELL` (기본: `claude`)
|
- `TMUX_SESSION_NAME` (기본: `claude`, `/start-claude` 대상)
|
||||||
|
- `CODEX_TMUX_SESSION_NAME` (기본: `codex`, `/start-codex` 대상)
|
||||||
- `PTY_READ_TIMEOUT` (기본: `5`)
|
- `PTY_READ_TIMEOUT` (기본: `5`)
|
||||||
- `OUTPUT_BUFFER_INTERVAL` (기본: `2.0`)
|
- `OUTPUT_BUFFER_INTERVAL` (기본: `2.0`)
|
||||||
- `MAX_MESSAGE_LENGTH` (기본: `3000`)
|
- `MAX_MESSAGE_LENGTH` (기본: `3000`)
|
||||||
@@ -51,10 +55,22 @@ cp .env.example .env
|
|||||||
- Slash Commands 생성:
|
- Slash Commands 생성:
|
||||||
- `/start-claude`
|
- `/start-claude`
|
||||||
- `/stop-claude`
|
- `/stop-claude`
|
||||||
|
- `/start-codex`
|
||||||
|
- `/stop-codex`
|
||||||
- 앱을 워크스페이스에 설치 후 토큰을 `.env`에 반영
|
- 앱을 워크스페이스에 설치 후 토큰을 `.env`에 반영
|
||||||
|
|
||||||
## 실행
|
## 실행
|
||||||
|
|
||||||
|
먼저 로컬에서 원하는 tmux 세션을 실행:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
tmux new -s claude claude
|
||||||
|
# 또는
|
||||||
|
tmux new -s codex codex
|
||||||
|
```
|
||||||
|
|
||||||
|
그 다음 브릿지 실행:
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
lazy-enter
|
lazy-enter
|
||||||
# 또는
|
# 또는
|
||||||
@@ -62,9 +78,9 @@ python -m lazy_enter
|
|||||||
```
|
```
|
||||||
|
|
||||||
실행 후 Slack의 허용된 채널에서:
|
실행 후 Slack의 허용된 채널에서:
|
||||||
- `/start-claude`: 세션 시작
|
- `/start-claude`, `/start-codex`: 기존 세션에 연결
|
||||||
- 일반 메시지 전송: Claude CLI로 입력 전달
|
- 일반 메시지 전송: 현재 연결된 CLI(Claude/Codex)로 입력 전달
|
||||||
- `/stop-claude`: 세션 종료
|
- `/stop-claude`, `/stop-codex`: 브릿지 연결 해제 (세션 유지)
|
||||||
|
|
||||||
## 테스트 및 품질 점검
|
## 테스트 및 품질 점검
|
||||||
|
|
||||||
|
|||||||
@@ -1,3 +1,3 @@
|
|||||||
"""LazyEnter - Slack을 통한 Claude Code 원격 제어 브릿지."""
|
"""LazyEnter - Slack을 통한 Claude/Codex 원격 제어 브릿지."""
|
||||||
|
|
||||||
__version__ = "0.1.0"
|
__version__ = "0.1.0"
|
||||||
|
|||||||
@@ -8,6 +8,7 @@ import threading
|
|||||||
import time
|
import time
|
||||||
|
|
||||||
from lazy_enter.config import Config
|
from lazy_enter.config import Config
|
||||||
|
from lazy_enter.output_filter import clean_terminal_output
|
||||||
from lazy_enter.pty_manager import PtyManager
|
from lazy_enter.pty_manager import PtyManager
|
||||||
from lazy_enter.slack_handler import SlackHandler
|
from lazy_enter.slack_handler import SlackHandler
|
||||||
|
|
||||||
@@ -24,6 +25,9 @@ class Bridge:
|
|||||||
self._output_thread: threading.Thread | None = None
|
self._output_thread: threading.Thread | None = None
|
||||||
self._running = False
|
self._running = False
|
||||||
self._channel: str = self.config.allowed_channel_id
|
self._channel: str = self.config.allowed_channel_id
|
||||||
|
self._active_target: str | None = None
|
||||||
|
self._last_sent_output: str = ""
|
||||||
|
self._last_sent_fingerprint: str | None = None
|
||||||
self._last_input_at = time.monotonic()
|
self._last_input_at = time.monotonic()
|
||||||
self._last_output_at = time.monotonic()
|
self._last_output_at = time.monotonic()
|
||||||
self._input_idle_reported = False
|
self._input_idle_reported = False
|
||||||
@@ -42,11 +46,14 @@ class Bridge:
|
|||||||
|
|
||||||
if self.pty and self.pty.is_alive:
|
if self.pty and self.pty.is_alive:
|
||||||
self.pty.send(text)
|
self.pty.send(text)
|
||||||
|
# 입력 이후 출력은 동일 문자열이어도 한 번 더 전달한다.
|
||||||
|
self._last_sent_output = ""
|
||||||
|
self._last_sent_fingerprint = None
|
||||||
self._last_input_at = time.monotonic()
|
self._last_input_at = time.monotonic()
|
||||||
self._input_idle_reported = False
|
self._input_idle_reported = False
|
||||||
logger.info("입력 전달: %s", text)
|
logger.info("입력 전달: %s", text)
|
||||||
else:
|
else:
|
||||||
self.slack.send_message(channel, ":warning: 실행 중인 세션이 없습니다.")
|
self.slack.send_message(channel, ":warning: 연결된 세션이 없습니다.")
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def _is_blocked_input(text: str) -> bool:
|
def _is_blocked_input(text: str) -> bool:
|
||||||
@@ -63,24 +70,51 @@ class Bridge:
|
|||||||
)
|
)
|
||||||
return any(pattern in normalized for pattern in blocked_patterns)
|
return any(pattern in normalized for pattern in blocked_patterns)
|
||||||
|
|
||||||
def _handle_command(self, command: str, channel: str) -> None:
|
@staticmethod
|
||||||
|
def _display_name(target: str) -> str:
|
||||||
|
if target == "codex":
|
||||||
|
return "Codex"
|
||||||
|
return "Claude"
|
||||||
|
|
||||||
|
def _session_name_for_target(self, target: str) -> str:
|
||||||
|
if target == "codex":
|
||||||
|
return self.config.codex_tmux_session_name
|
||||||
|
return self.config.tmux_session_name
|
||||||
|
|
||||||
|
def _handle_command(self, action: str, target: str, channel: str) -> None:
|
||||||
"""슬래시 커맨드를 처리한다."""
|
"""슬래시 커맨드를 처리한다."""
|
||||||
if command == "start":
|
if target not in {"claude", "codex"}:
|
||||||
self._start_session(channel)
|
self.slack.send_message(channel, ":warning: 지원하지 않는 대상입니다.")
|
||||||
elif command == "stop":
|
return
|
||||||
|
|
||||||
|
if action == "start":
|
||||||
|
self._start_session(channel, target)
|
||||||
|
elif action == "stop":
|
||||||
self._stop_session(channel)
|
self._stop_session(channel)
|
||||||
|
|
||||||
def _start_session(self, channel: str) -> None:
|
def _start_session(self, channel: str, target: str) -> None:
|
||||||
"""Claude Code 세션을 시작한다."""
|
"""지정한 대상의 tmux 세션에 연결한다."""
|
||||||
if self.pty and self.pty.is_alive:
|
if self.pty and self.pty.is_alive:
|
||||||
self.slack.send_message(
|
self.slack.send_message(
|
||||||
channel, ":information_source: 이미 세션이 실행 중입니다."
|
channel, ":information_source: 이미 세션에 연결되어 있습니다."
|
||||||
)
|
)
|
||||||
return
|
return
|
||||||
|
|
||||||
self._channel = channel
|
self._channel = channel
|
||||||
self.pty = PtyManager(self.config.default_shell)
|
self._active_target = target
|
||||||
self.pty.start()
|
self._last_sent_output = ""
|
||||||
|
self._last_sent_fingerprint = None
|
||||||
|
self.pty = PtyManager(
|
||||||
|
self._session_name_for_target(target),
|
||||||
|
cli_name=target,
|
||||||
|
)
|
||||||
|
try:
|
||||||
|
self.pty.start()
|
||||||
|
except RuntimeError as exc:
|
||||||
|
self.pty = None
|
||||||
|
self.slack.send_message(channel, f":warning: {exc}")
|
||||||
|
return
|
||||||
|
|
||||||
self._running = True
|
self._running = True
|
||||||
self._last_input_at = time.monotonic()
|
self._last_input_at = time.monotonic()
|
||||||
self._last_output_at = time.monotonic()
|
self._last_output_at = time.monotonic()
|
||||||
@@ -89,15 +123,21 @@ class Bridge:
|
|||||||
self._output_thread = threading.Thread(target=self._poll_output, daemon=True)
|
self._output_thread = threading.Thread(target=self._poll_output, daemon=True)
|
||||||
self._output_thread.start()
|
self._output_thread.start()
|
||||||
|
|
||||||
self.slack.send_message(channel, ":rocket: Claude Code 세션이 시작되었습니다.")
|
display_name = self._display_name(target)
|
||||||
|
self.slack.send_message(
|
||||||
|
channel, f":link: {display_name} 세션에 연결되었습니다."
|
||||||
|
)
|
||||||
|
|
||||||
def _stop_session(self, channel: str) -> None:
|
def _stop_session(self, channel: str) -> None:
|
||||||
"""Claude Code 세션을 종료한다."""
|
"""브릿지 연결만 해제한다."""
|
||||||
self._running = False
|
self._running = False
|
||||||
if self.pty:
|
if self.pty:
|
||||||
self.pty.stop()
|
self.pty.stop()
|
||||||
self.pty = None
|
self.pty = None
|
||||||
self.slack.send_message(channel, ":stop_sign: 세션이 종료되었습니다.")
|
self._active_target = None
|
||||||
|
self._last_sent_output = ""
|
||||||
|
self._last_sent_fingerprint = None
|
||||||
|
self.slack.send_message(channel, ":electric_plug: 세션 연결이 해제되었습니다.")
|
||||||
|
|
||||||
def _poll_output(self) -> None:
|
def _poll_output(self) -> None:
|
||||||
"""PTY 출력을 주기적으로 읽어 Slack으로 전송한다."""
|
"""PTY 출력을 주기적으로 읽어 Slack으로 전송한다."""
|
||||||
@@ -106,16 +146,14 @@ class Bridge:
|
|||||||
now = time.monotonic()
|
now = time.monotonic()
|
||||||
output = self.pty.read_output(timeout=self.config.pty_read_timeout)
|
output = self.pty.read_output(timeout=self.config.pty_read_timeout)
|
||||||
if output:
|
if output:
|
||||||
buffer += output
|
cleaned = clean_terminal_output(output)
|
||||||
self._last_output_at = now
|
if cleaned:
|
||||||
self._output_idle_reported = False
|
buffer += f"{cleaned}\n"
|
||||||
|
self._last_output_at = now
|
||||||
|
self._output_idle_reported = False
|
||||||
|
|
||||||
if buffer:
|
if buffer:
|
||||||
# 메시지 길이 제한 적용
|
self._send_output_chunks(buffer)
|
||||||
message = buffer[: self.config.max_message_length]
|
|
||||||
if len(buffer) > self.config.max_message_length:
|
|
||||||
message += "\n... (truncated)"
|
|
||||||
self.slack.send_message(self._channel, f"```\n{message}\n```")
|
|
||||||
buffer = ""
|
buffer = ""
|
||||||
|
|
||||||
output_idle = now - self._last_output_at
|
output_idle = now - self._last_output_at
|
||||||
@@ -150,8 +188,85 @@ class Bridge:
|
|||||||
if not self._running:
|
if not self._running:
|
||||||
return
|
return
|
||||||
|
|
||||||
# 프로세스가 예기치 않게 종료된 경우
|
# attach 프로세스가 예기치 않게 종료된 경우
|
||||||
self.slack.send_message(self._channel, ":warning: 프로세스가 종료되었습니다.")
|
self.slack.send_message(self._channel, ":warning: 세션 연결이 종료되었습니다.")
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def _split_message(text: str, max_length: int) -> list[str]:
|
||||||
|
"""긴 텍스트를 메시지 길이 제한에 맞게 분할한다."""
|
||||||
|
if max_length <= 0:
|
||||||
|
return [text] if text else []
|
||||||
|
|
||||||
|
lines = text.splitlines(keepends=True)
|
||||||
|
chunks: list[str] = []
|
||||||
|
current = ""
|
||||||
|
|
||||||
|
def flush() -> None:
|
||||||
|
nonlocal current
|
||||||
|
if current:
|
||||||
|
chunks.append(current)
|
||||||
|
current = ""
|
||||||
|
|
||||||
|
for line in lines:
|
||||||
|
if len(line) > max_length:
|
||||||
|
flush()
|
||||||
|
start = 0
|
||||||
|
while start < len(line):
|
||||||
|
piece = line[start : start + max_length]
|
||||||
|
chunks.append(piece)
|
||||||
|
start += max_length
|
||||||
|
continue
|
||||||
|
|
||||||
|
if len(current) + len(line) > max_length:
|
||||||
|
flush()
|
||||||
|
current += line
|
||||||
|
|
||||||
|
flush()
|
||||||
|
return chunks
|
||||||
|
|
||||||
|
def _send_output_chunks(self, text: str) -> None:
|
||||||
|
"""출력을 잘라서 Slack으로 순차 전송한다."""
|
||||||
|
chunks = self._split_message(text, self.config.max_message_length)
|
||||||
|
snapshot = "\n\x00".join(chunks)
|
||||||
|
fingerprint = self._output_fingerprint(snapshot)
|
||||||
|
if not chunks:
|
||||||
|
return
|
||||||
|
if (
|
||||||
|
self._last_sent_fingerprint is not None
|
||||||
|
and fingerprint == self._last_sent_fingerprint
|
||||||
|
):
|
||||||
|
return
|
||||||
|
|
||||||
|
for chunk in chunks:
|
||||||
|
if chunk.endswith("\n"):
|
||||||
|
message = f"```\n{chunk}```"
|
||||||
|
else:
|
||||||
|
message = f"```\n{chunk}\n```"
|
||||||
|
self.slack.send_message(self._channel, message)
|
||||||
|
self._last_sent_output = snapshot
|
||||||
|
self._last_sent_fingerprint = fingerprint
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def _output_fingerprint(text: str) -> str:
|
||||||
|
"""중복 전송 억제를 위한 정규화 지문을 생성한다."""
|
||||||
|
normalized_lines: list[str] = []
|
||||||
|
for raw_line in text.splitlines():
|
||||||
|
line = raw_line.rstrip()
|
||||||
|
if not line.strip():
|
||||||
|
continue
|
||||||
|
|
||||||
|
# tmux 상태줄의 시계/날짜 라인은 프레임마다 변할 수 있어 제외한다.
|
||||||
|
if re.fullmatch(
|
||||||
|
(
|
||||||
|
r'\s*\w*odex\]\s+\d+:[^\[]*'
|
||||||
|
r'\[\d+,\d+\]\s+".+"\s+\d{2}:\d{2}\s+\d{2}-[A-Za-z]{3}-\d{2}'
|
||||||
|
),
|
||||||
|
line,
|
||||||
|
):
|
||||||
|
continue
|
||||||
|
|
||||||
|
normalized_lines.append(line)
|
||||||
|
return "\n".join(normalized_lines)
|
||||||
|
|
||||||
def run(self) -> None:
|
def run(self) -> None:
|
||||||
"""브릿지를 시작한다."""
|
"""브릿지를 시작한다."""
|
||||||
|
|||||||
@@ -18,8 +18,9 @@ class Config:
|
|||||||
allowed_user_id: str = os.getenv("SLACK_ALLOWED_USER_ID", "")
|
allowed_user_id: str = os.getenv("SLACK_ALLOWED_USER_ID", "")
|
||||||
allowed_channel_id: str = os.getenv("SLACK_ALLOWED_CHANNEL_ID", "")
|
allowed_channel_id: str = os.getenv("SLACK_ALLOWED_CHANNEL_ID", "")
|
||||||
|
|
||||||
# PTY
|
# PTY / tmux attach
|
||||||
default_shell: str = os.getenv("DEFAULT_SHELL", "claude")
|
tmux_session_name: str = os.getenv("TMUX_SESSION_NAME", "claude")
|
||||||
|
codex_tmux_session_name: str = os.getenv("CODEX_TMUX_SESSION_NAME", "codex")
|
||||||
pty_read_timeout: int = int(os.getenv("PTY_READ_TIMEOUT", "5"))
|
pty_read_timeout: int = int(os.getenv("PTY_READ_TIMEOUT", "5"))
|
||||||
|
|
||||||
# Buffer
|
# Buffer
|
||||||
|
|||||||
81
src/lazy_enter/output_filter.py
Normal file
81
src/lazy_enter/output_filter.py
Normal file
@@ -0,0 +1,81 @@
|
|||||||
|
"""PTY 출력에서 터미널 제어 시퀀스를 제거한다."""
|
||||||
|
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
import re
|
||||||
|
|
||||||
|
# ANSI CSI / OSC / DCS / 단일 ESC 시퀀스를 폭넓게 제거한다.
|
||||||
|
_ANSI_ESCAPE_RE = re.compile(
|
||||||
|
r"(?:\x1B\[[0-?]*[ -/]*[@-~])"
|
||||||
|
r"|(?:\x1B\][^\x1B\x07]*(?:\x07|\x1B\\))"
|
||||||
|
r"|(?:\x1BP[^\x1B]*(?:\x1B\\))"
|
||||||
|
r"|(?:\x1B[@-_])"
|
||||||
|
)
|
||||||
|
|
||||||
|
# ESC 문자가 유실된 상태로 남는 CSI 토큰(예: [38;2;153;153;153m) 제거.
|
||||||
|
_BROKEN_CSI_RE = re.compile(r"\[(?:[0-9;<=>?]|(?:\d+;))*\d*[A-Za-z]")
|
||||||
|
|
||||||
|
# ESC(B 같은 문자셋 지정 시퀀스가 ESC 유실 후 남긴 토큰 제거.
|
||||||
|
_BROKEN_CHARSET_RE = re.compile(r"[\(\)][B0]")
|
||||||
|
|
||||||
|
# C0 제어문자 중 개행/탭/캐리지리턴을 제외하고 제거.
|
||||||
|
_CONTROL_RE = re.compile(r"[\x00-\x08\x0B-\x1F\x7F]")
|
||||||
|
|
||||||
|
_CODEX_TUI_LINE_PATTERNS = (
|
||||||
|
re.compile(r"openai codex", re.IGNORECASE),
|
||||||
|
re.compile(r"/model to change", re.IGNORECASE),
|
||||||
|
re.compile(r"^\s*[|│]\s*model:", re.IGNORECASE),
|
||||||
|
re.compile(r"^\s*[|│]\s*directory:", re.IGNORECASE),
|
||||||
|
re.compile(r"^\s*tip:\s*new\s+codex", re.IGNORECASE),
|
||||||
|
re.compile(r"find and fix a bug in @filename", re.IGNORECASE),
|
||||||
|
re.compile(r"\?\s*for shortcuts", re.IGNORECASE),
|
||||||
|
re.compile(r"\bcontext left\b", re.IGNORECASE),
|
||||||
|
re.compile(r"^\s*\w*odex\]\s+\d+:", re.IGNORECASE),
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def _is_box_border_line(line: str) -> bool:
|
||||||
|
stripped = line.strip()
|
||||||
|
if not stripped:
|
||||||
|
return False
|
||||||
|
# ESC(B 유실 잔재가 앞에 붙는 경우(예: =╭────╮)도 border로 본다.
|
||||||
|
stripped = stripped.lstrip("= ")
|
||||||
|
return all(ch in "╭╮╰╯─│┌┐└┘├┤┬┴┼" for ch in stripped)
|
||||||
|
|
||||||
|
|
||||||
|
def _is_box_empty_line(line: str) -> bool:
|
||||||
|
stripped = line.strip()
|
||||||
|
if not stripped:
|
||||||
|
return False
|
||||||
|
return bool(re.fullmatch(r"[│|]\s+[│|]", stripped))
|
||||||
|
|
||||||
|
|
||||||
|
def _is_noise_line(line: str) -> bool:
|
||||||
|
if _is_box_border_line(line):
|
||||||
|
return True
|
||||||
|
if _is_box_empty_line(line):
|
||||||
|
return True
|
||||||
|
return any(pattern.search(line) for pattern in _CODEX_TUI_LINE_PATTERNS)
|
||||||
|
|
||||||
|
|
||||||
|
def clean_terminal_output(text: str) -> str:
|
||||||
|
"""Slack 전송용 텍스트를 정리한다."""
|
||||||
|
cleaned = _ANSI_ESCAPE_RE.sub("", text)
|
||||||
|
cleaned = _BROKEN_CSI_RE.sub("", cleaned)
|
||||||
|
cleaned = _BROKEN_CHARSET_RE.sub("", cleaned)
|
||||||
|
cleaned = cleaned.replace("\r", "")
|
||||||
|
cleaned = _CONTROL_RE.sub("", cleaned)
|
||||||
|
|
||||||
|
lines: list[str] = []
|
||||||
|
for raw_line in cleaned.splitlines():
|
||||||
|
line = raw_line.rstrip()
|
||||||
|
if not line:
|
||||||
|
continue
|
||||||
|
if _is_noise_line(line):
|
||||||
|
continue
|
||||||
|
if lines and lines[-1] == line:
|
||||||
|
continue
|
||||||
|
lines.append(line)
|
||||||
|
|
||||||
|
compact = "\n".join(lines).strip()
|
||||||
|
return compact
|
||||||
@@ -1,8 +1,9 @@
|
|||||||
"""pexpect 기반 PTY 프로세스 관리."""
|
"""기존 tmux 세션에 attach하여 CLI 입출력을 중계한다."""
|
||||||
|
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
import logging
|
import logging
|
||||||
|
import subprocess
|
||||||
|
|
||||||
import pexpect
|
import pexpect
|
||||||
|
|
||||||
@@ -10,21 +11,38 @@ logger = logging.getLogger(__name__)
|
|||||||
|
|
||||||
|
|
||||||
class PtyManager:
|
class PtyManager:
|
||||||
"""가상 터미널에서 CLI 프로세스를 생성하고 입출력을 제어한다."""
|
"""기존 tmux 세션에 attach하고 입출력을 제어한다."""
|
||||||
|
|
||||||
def __init__(self, command: str = "claude") -> None:
|
def __init__(self, session_name: str = "claude", cli_name: str = "claude") -> None:
|
||||||
self.command = command
|
self.session_name = session_name
|
||||||
|
self.cli_name = cli_name
|
||||||
self._process: pexpect.spawn | None = None
|
self._process: pexpect.spawn | None = None
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def is_alive(self) -> bool:
|
def is_alive(self) -> bool:
|
||||||
return self._process is not None and self._process.isalive()
|
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} {self.cli_name}"
|
||||||
|
)
|
||||||
|
|
||||||
def start(self) -> None:
|
def start(self) -> None:
|
||||||
"""프로세스를 시작한다."""
|
"""기존 tmux 세션에 attach한다."""
|
||||||
logger.info("프로세스 시작: %s", self.command)
|
self._ensure_session_exists()
|
||||||
|
logger.info("tmux 세션 attach: %s", self.session_name)
|
||||||
self._process = pexpect.spawn(
|
self._process = pexpect.spawn(
|
||||||
self.command,
|
"tmux",
|
||||||
|
["attach-session", "-t", self.session_name],
|
||||||
encoding="utf-8",
|
encoding="utf-8",
|
||||||
timeout=None,
|
timeout=None,
|
||||||
)
|
)
|
||||||
@@ -49,8 +67,8 @@ class PtyManager:
|
|||||||
return self._process.before or ""
|
return self._process.before or ""
|
||||||
|
|
||||||
def stop(self) -> None:
|
def stop(self) -> None:
|
||||||
"""프로세스를 종료한다."""
|
"""attach 연결만 종료한다(원격 세션은 유지)."""
|
||||||
if self._process is not None:
|
if self._process is not None:
|
||||||
logger.info("프로세스 종료")
|
logger.info("tmux attach 종료")
|
||||||
self._process.close(force=True)
|
self._process.close(force=True)
|
||||||
self._process = None
|
self._process = None
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ from __future__ import annotations
|
|||||||
|
|
||||||
import logging
|
import logging
|
||||||
import time
|
import time
|
||||||
|
from collections.abc import Callable
|
||||||
|
|
||||||
from slack_bolt import App
|
from slack_bolt import App
|
||||||
from slack_bolt.adapter.socket_mode import SocketModeHandler
|
from slack_bolt.adapter.socket_mode import SocketModeHandler
|
||||||
@@ -22,8 +23,8 @@ class SlackHandler:
|
|||||||
self.app = App(token=config.slack_bot_token)
|
self.app = App(token=config.slack_bot_token)
|
||||||
self._handler: SocketModeHandler | None = None
|
self._handler: SocketModeHandler | None = None
|
||||||
self._stop_requested = False
|
self._stop_requested = False
|
||||||
self._on_message_callback: callable | None = None
|
self._on_message_callback: Callable[[str, str], None] | None = None
|
||||||
self._on_command_callback: callable | None = None
|
self._on_command_callback: Callable[[str, str, str], None] | None = None
|
||||||
|
|
||||||
self._register_listeners()
|
self._register_listeners()
|
||||||
|
|
||||||
@@ -46,7 +47,7 @@ class SlackHandler:
|
|||||||
"""Slack 이벤트 리스너를 등록한다."""
|
"""Slack 이벤트 리스너를 등록한다."""
|
||||||
|
|
||||||
@self.app.event("message")
|
@self.app.event("message")
|
||||||
def handle_message(event: dict, say: callable) -> None:
|
def handle_message(event: dict, say: Callable[..., None]) -> None:
|
||||||
user_id = event.get("user", "")
|
user_id = event.get("user", "")
|
||||||
channel_id = event.get("channel", "")
|
channel_id = event.get("channel", "")
|
||||||
text = event.get("text", "")
|
text = event.get("text", "")
|
||||||
@@ -58,7 +59,7 @@ class SlackHandler:
|
|||||||
self._on_message_callback(text, channel_id)
|
self._on_message_callback(text, channel_id)
|
||||||
|
|
||||||
@self.app.command("/start-claude")
|
@self.app.command("/start-claude")
|
||||||
def handle_start(ack: callable, body: dict) -> None:
|
def handle_start_claude(ack: Callable[..., None], body: dict) -> None:
|
||||||
ack()
|
ack()
|
||||||
user_id = body.get("user_id", "")
|
user_id = body.get("user_id", "")
|
||||||
channel_id = body.get("channel_id", "")
|
channel_id = body.get("channel_id", "")
|
||||||
@@ -67,10 +68,10 @@ class SlackHandler:
|
|||||||
return
|
return
|
||||||
|
|
||||||
if self._on_command_callback:
|
if self._on_command_callback:
|
||||||
self._on_command_callback("start", channel_id)
|
self._on_command_callback("start", "claude", channel_id)
|
||||||
|
|
||||||
@self.app.command("/stop-claude")
|
@self.app.command("/stop-claude")
|
||||||
def handle_stop(ack: callable, body: dict) -> None:
|
def handle_stop_claude(ack: Callable[..., None], body: dict) -> None:
|
||||||
ack()
|
ack()
|
||||||
user_id = body.get("user_id", "")
|
user_id = body.get("user_id", "")
|
||||||
channel_id = body.get("channel_id", "")
|
channel_id = body.get("channel_id", "")
|
||||||
@@ -79,13 +80,37 @@ class SlackHandler:
|
|||||||
return
|
return
|
||||||
|
|
||||||
if self._on_command_callback:
|
if self._on_command_callback:
|
||||||
self._on_command_callback("stop", channel_id)
|
self._on_command_callback("stop", "claude", channel_id)
|
||||||
|
|
||||||
def on_message(self, callback: callable) -> None:
|
@self.app.command("/start-codex")
|
||||||
|
def handle_start_codex(ack: Callable[..., None], body: dict) -> None:
|
||||||
|
ack()
|
||||||
|
user_id = body.get("user_id", "")
|
||||||
|
channel_id = body.get("channel_id", "")
|
||||||
|
|
||||||
|
if not self._is_authorized(user_id, channel_id):
|
||||||
|
return
|
||||||
|
|
||||||
|
if self._on_command_callback:
|
||||||
|
self._on_command_callback("start", "codex", channel_id)
|
||||||
|
|
||||||
|
@self.app.command("/stop-codex")
|
||||||
|
def handle_stop_codex(ack: Callable[..., None], body: dict) -> None:
|
||||||
|
ack()
|
||||||
|
user_id = body.get("user_id", "")
|
||||||
|
channel_id = body.get("channel_id", "")
|
||||||
|
|
||||||
|
if not self._is_authorized(user_id, channel_id):
|
||||||
|
return
|
||||||
|
|
||||||
|
if self._on_command_callback:
|
||||||
|
self._on_command_callback("stop", "codex", channel_id)
|
||||||
|
|
||||||
|
def on_message(self, callback: Callable[[str, str], None]) -> None:
|
||||||
"""메시지 수신 콜백을 등록한다."""
|
"""메시지 수신 콜백을 등록한다."""
|
||||||
self._on_message_callback = callback
|
self._on_message_callback = callback
|
||||||
|
|
||||||
def on_command(self, callback: callable) -> None:
|
def on_command(self, callback: Callable[[str, str, str], None]) -> None:
|
||||||
"""슬래시 커맨드 콜백을 등록한다."""
|
"""슬래시 커맨드 콜백을 등록한다."""
|
||||||
self._on_command_callback = callback
|
self._on_command_callback = callback
|
||||||
|
|
||||||
|
|||||||
299
tests/test_bridge.py
Normal file
299
tests/test_bridge.py
Normal file
@@ -0,0 +1,299 @@
|
|||||||
|
"""Bridge 명령 라우팅 테스트."""
|
||||||
|
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
import sys
|
||||||
|
import types
|
||||||
|
|
||||||
|
slack_bolt = types.ModuleType("slack_bolt")
|
||||||
|
slack_bolt.App = object
|
||||||
|
socket_mode = types.ModuleType("slack_bolt.adapter.socket_mode")
|
||||||
|
socket_mode.SocketModeHandler = object
|
||||||
|
slack_sdk = types.ModuleType("slack_sdk")
|
||||||
|
slack_sdk.WebClient = object
|
||||||
|
sys.modules.setdefault("slack_bolt", slack_bolt)
|
||||||
|
sys.modules.setdefault("slack_bolt.adapter.socket_mode", socket_mode)
|
||||||
|
sys.modules.setdefault("slack_sdk", slack_sdk)
|
||||||
|
|
||||||
|
|
||||||
|
class FakeSlackHandler:
|
||||||
|
def __init__(self, _config) -> None:
|
||||||
|
self.message_callback = None
|
||||||
|
self.command_callback = None
|
||||||
|
self.sent_messages: list[tuple[str, str]] = []
|
||||||
|
|
||||||
|
def on_message(self, callback) -> None:
|
||||||
|
self.message_callback = callback
|
||||||
|
|
||||||
|
def on_command(self, callback) -> None:
|
||||||
|
self.command_callback = callback
|
||||||
|
|
||||||
|
def send_message(self, channel: str, text: str) -> None:
|
||||||
|
self.sent_messages.append((channel, text))
|
||||||
|
|
||||||
|
def start(self) -> None:
|
||||||
|
return None
|
||||||
|
|
||||||
|
def stop(self) -> None:
|
||||||
|
return None
|
||||||
|
|
||||||
|
|
||||||
|
class FakePtyManager:
|
||||||
|
instances: list[FakePtyManager] = []
|
||||||
|
|
||||||
|
def __init__(self, session_name: str, cli_name: str = "claude") -> None:
|
||||||
|
self.session_name = session_name
|
||||||
|
self.cli_name = cli_name
|
||||||
|
self._alive = False
|
||||||
|
self.sent_inputs: list[str] = []
|
||||||
|
FakePtyManager.instances.append(self)
|
||||||
|
|
||||||
|
@property
|
||||||
|
def is_alive(self) -> bool:
|
||||||
|
return self._alive
|
||||||
|
|
||||||
|
def start(self) -> None:
|
||||||
|
self._alive = True
|
||||||
|
|
||||||
|
def stop(self) -> None:
|
||||||
|
self._alive = False
|
||||||
|
|
||||||
|
def send(self, text: str) -> None:
|
||||||
|
self.sent_inputs.append(text)
|
||||||
|
|
||||||
|
def read_output(self, timeout: int = 5) -> str:
|
||||||
|
return ""
|
||||||
|
|
||||||
|
|
||||||
|
def _make_bridge(monkeypatch):
|
||||||
|
from lazy_enter.bridge import Bridge
|
||||||
|
from lazy_enter.config import Config
|
||||||
|
|
||||||
|
monkeypatch.setattr("lazy_enter.bridge.SlackHandler", FakeSlackHandler)
|
||||||
|
monkeypatch.setattr("lazy_enter.bridge.PtyManager", FakePtyManager)
|
||||||
|
config = Config()
|
||||||
|
config.tmux_session_name = "claude-room"
|
||||||
|
config.codex_tmux_session_name = "codex-room"
|
||||||
|
return Bridge(config)
|
||||||
|
|
||||||
|
|
||||||
|
def test_start_claude_routes_to_claude_session(monkeypatch) -> None:
|
||||||
|
FakePtyManager.instances.clear()
|
||||||
|
bridge = _make_bridge(monkeypatch)
|
||||||
|
|
||||||
|
bridge._handle_command("start", "claude", "C1")
|
||||||
|
|
||||||
|
assert FakePtyManager.instances[-1].session_name == "claude-room"
|
||||||
|
assert FakePtyManager.instances[-1].cli_name == "claude"
|
||||||
|
assert bridge.slack.sent_messages[-1] == (
|
||||||
|
"C1",
|
||||||
|
":link: Claude 세션에 연결되었습니다.",
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def test_start_codex_routes_to_codex_session(monkeypatch) -> None:
|
||||||
|
FakePtyManager.instances.clear()
|
||||||
|
bridge = _make_bridge(monkeypatch)
|
||||||
|
|
||||||
|
bridge._handle_command("start", "codex", "C1")
|
||||||
|
|
||||||
|
assert FakePtyManager.instances[-1].session_name == "codex-room"
|
||||||
|
assert FakePtyManager.instances[-1].cli_name == "codex"
|
||||||
|
assert bridge.slack.sent_messages[-1] == (
|
||||||
|
"C1",
|
||||||
|
":link: Codex 세션에 연결되었습니다.",
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def test_start_session_resets_output_dedup_state(monkeypatch) -> None:
|
||||||
|
FakePtyManager.instances.clear()
|
||||||
|
bridge = _make_bridge(monkeypatch)
|
||||||
|
bridge._last_sent_output = "stale"
|
||||||
|
bridge._last_sent_fingerprint = "stale-fp"
|
||||||
|
|
||||||
|
bridge._handle_command("start", "codex", "C1")
|
||||||
|
|
||||||
|
assert bridge._last_sent_output == ""
|
||||||
|
assert bridge._last_sent_fingerprint is None
|
||||||
|
|
||||||
|
|
||||||
|
def test_unknown_target_is_rejected(monkeypatch) -> None:
|
||||||
|
bridge = _make_bridge(monkeypatch)
|
||||||
|
|
||||||
|
bridge._handle_command("start", "unknown", "C1")
|
||||||
|
|
||||||
|
assert bridge.slack.sent_messages[-1] == (
|
||||||
|
"C1",
|
||||||
|
":warning: 지원하지 않는 대상입니다.",
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def test_handle_message_resets_last_sent_output_after_input(monkeypatch) -> None:
|
||||||
|
FakePtyManager.instances.clear()
|
||||||
|
bridge = _make_bridge(monkeypatch)
|
||||||
|
|
||||||
|
bridge._handle_command("start", "codex", "C1")
|
||||||
|
pty = FakePtyManager.instances[-1]
|
||||||
|
bridge._last_sent_output = "previous output"
|
||||||
|
|
||||||
|
bridge._handle_message("status", "C1")
|
||||||
|
|
||||||
|
assert pty.sent_inputs[-1] == "status"
|
||||||
|
assert bridge._last_sent_output == ""
|
||||||
|
assert bridge._last_sent_fingerprint is None
|
||||||
|
|
||||||
|
|
||||||
|
def test_split_message_preserves_all_content(monkeypatch) -> None:
|
||||||
|
bridge = _make_bridge(monkeypatch)
|
||||||
|
chunks = bridge._split_message("line1\nline2\nline3", max_length=7)
|
||||||
|
assert "".join(chunks) == "line1\nline2\nline3"
|
||||||
|
|
||||||
|
|
||||||
|
def test_split_message_preserves_trailing_whitespace_and_blank_line(
|
||||||
|
monkeypatch,
|
||||||
|
) -> None:
|
||||||
|
bridge = _make_bridge(monkeypatch)
|
||||||
|
text = "ab \n\ncd "
|
||||||
|
chunks = bridge._split_message(text, max_length=4)
|
||||||
|
assert "".join(chunks) == text
|
||||||
|
|
||||||
|
|
||||||
|
def test_send_output_chunks_sends_multiple_messages(monkeypatch) -> None:
|
||||||
|
bridge = _make_bridge(monkeypatch)
|
||||||
|
bridge._channel = "C1"
|
||||||
|
bridge._send_output_chunks("1234567890")
|
||||||
|
|
||||||
|
assert bridge.slack.sent_messages == [
|
||||||
|
("C1", "```\n1234567890\n```"),
|
||||||
|
]
|
||||||
|
|
||||||
|
bridge.config.max_message_length = 4
|
||||||
|
bridge.slack.sent_messages.clear()
|
||||||
|
bridge._send_output_chunks("abcdefghij")
|
||||||
|
assert bridge.slack.sent_messages == [
|
||||||
|
("C1", "```\nabcd\n```"),
|
||||||
|
("C1", "```\nefgh\n```"),
|
||||||
|
("C1", "```\nij\n```"),
|
||||||
|
]
|
||||||
|
|
||||||
|
|
||||||
|
def test_send_output_chunks_skips_duplicate_snapshot(monkeypatch) -> None:
|
||||||
|
bridge = _make_bridge(monkeypatch)
|
||||||
|
bridge._channel = "C1"
|
||||||
|
bridge.config.max_message_length = 4
|
||||||
|
|
||||||
|
bridge._send_output_chunks("abcdefghij")
|
||||||
|
first = list(bridge.slack.sent_messages)
|
||||||
|
|
||||||
|
bridge._send_output_chunks("abcdefghij")
|
||||||
|
assert bridge.slack.sent_messages == first
|
||||||
|
|
||||||
|
|
||||||
|
def test_send_output_chunks_skips_duplicate_with_volatile_status_line(
|
||||||
|
monkeypatch,
|
||||||
|
) -> None:
|
||||||
|
bridge = _make_bridge(monkeypatch)
|
||||||
|
bridge._channel = "C1"
|
||||||
|
bridge.config.max_message_length = 1000
|
||||||
|
|
||||||
|
first_output = (
|
||||||
|
"Would you like to make the following edits?\n"
|
||||||
|
"src/main.py (+68 -1)\n"
|
||||||
|
'odex] 0:node* [0,0] "jihoson-home" 05:12 17-Feb-26\n'
|
||||||
|
"Press enter to confirm or esc to cancel\n"
|
||||||
|
)
|
||||||
|
second_output = (
|
||||||
|
"Would you like to make the following edits?\n"
|
||||||
|
"src/main.py (+68 -1)\n"
|
||||||
|
'odex] 0:node* [0,0] "jihoson-home" 05:13 17-Feb-26\n'
|
||||||
|
"Press enter to confirm or esc to cancel\n"
|
||||||
|
)
|
||||||
|
|
||||||
|
bridge._send_output_chunks(first_output)
|
||||||
|
first = list(bridge.slack.sent_messages)
|
||||||
|
|
||||||
|
bridge._send_output_chunks(second_output)
|
||||||
|
assert bridge.slack.sent_messages == first
|
||||||
|
|
||||||
|
|
||||||
|
def test_send_output_chunks_sends_first_when_fingerprint_is_empty(monkeypatch) -> None:
|
||||||
|
bridge = _make_bridge(monkeypatch)
|
||||||
|
bridge._channel = "C1"
|
||||||
|
volatile_only = 'odex] 0:node* [0,0] "jihoson-home" 05:12 17-Feb-26\n'
|
||||||
|
|
||||||
|
bridge._send_output_chunks(volatile_only)
|
||||||
|
assert bridge.slack.sent_messages == [("C1", f"```\n{volatile_only.rstrip()}\n```")]
|
||||||
|
|
||||||
|
bridge._send_output_chunks(volatile_only)
|
||||||
|
assert bridge.slack.sent_messages == [("C1", f"```\n{volatile_only.rstrip()}\n```")]
|
||||||
|
|
||||||
|
|
||||||
|
def test_send_output_chunks_keeps_non_tmux_timestamp_lines(monkeypatch) -> None:
|
||||||
|
bridge = _make_bridge(monkeypatch)
|
||||||
|
bridge._channel = "C1"
|
||||||
|
bridge.config.max_message_length = 1000
|
||||||
|
|
||||||
|
first_output = (
|
||||||
|
"report generated at 05:12 17-Feb-26\n"
|
||||||
|
"count=10\n"
|
||||||
|
)
|
||||||
|
second_output = (
|
||||||
|
"report generated at 05:13 17-Feb-26\n"
|
||||||
|
"count=10\n"
|
||||||
|
)
|
||||||
|
|
||||||
|
bridge._send_output_chunks(first_output)
|
||||||
|
bridge._send_output_chunks(second_output)
|
||||||
|
|
||||||
|
assert bridge.slack.sent_messages == [
|
||||||
|
("C1", f"```\n{first_output.rstrip()}\n```"),
|
||||||
|
("C1", f"```\n{second_output.rstrip()}\n```"),
|
||||||
|
]
|
||||||
|
|
||||||
|
|
||||||
|
def test_send_output_chunks_preserves_whitespace_signals(monkeypatch) -> None:
|
||||||
|
bridge = _make_bridge(monkeypatch)
|
||||||
|
bridge._channel = "C1"
|
||||||
|
bridge.config.max_message_length = 1000
|
||||||
|
|
||||||
|
first_output = "def f():\n return 1\n"
|
||||||
|
second_output = "def f():\n return 1\n"
|
||||||
|
|
||||||
|
bridge._send_output_chunks(first_output)
|
||||||
|
bridge._send_output_chunks(second_output)
|
||||||
|
|
||||||
|
assert bridge.slack.sent_messages == [
|
||||||
|
("C1", f"```\n{first_output.rstrip()}\n```"),
|
||||||
|
("C1", f"```\n{second_output.rstrip()}\n```"),
|
||||||
|
]
|
||||||
|
|
||||||
|
|
||||||
|
def test_send_output_chunks_keeps_standalone_time_lines(monkeypatch) -> None:
|
||||||
|
bridge = _make_bridge(monkeypatch)
|
||||||
|
bridge._channel = "C1"
|
||||||
|
bridge.config.max_message_length = 1000
|
||||||
|
|
||||||
|
bridge._send_output_chunks("[5:12 AM]\n")
|
||||||
|
bridge._send_output_chunks("[5:13 AM]\n")
|
||||||
|
|
||||||
|
assert bridge.slack.sent_messages == [
|
||||||
|
("C1", "```\n[5:12 AM]\n```"),
|
||||||
|
("C1", "```\n[5:13 AM]\n```"),
|
||||||
|
]
|
||||||
|
|
||||||
|
|
||||||
|
def test_send_output_chunks_keeps_non_tmux_status_like_lines(monkeypatch) -> None:
|
||||||
|
bridge = _make_bridge(monkeypatch)
|
||||||
|
bridge._channel = "C1"
|
||||||
|
bridge.config.max_message_length = 1000
|
||||||
|
|
||||||
|
first_output = '[2,3] "job-runner" 05:12 17-Feb-26\n'
|
||||||
|
second_output = '[2,3] "job-runner" 05:13 17-Feb-26\n'
|
||||||
|
|
||||||
|
bridge._send_output_chunks(first_output)
|
||||||
|
bridge._send_output_chunks(second_output)
|
||||||
|
|
||||||
|
assert bridge.slack.sent_messages == [
|
||||||
|
("C1", "```\n[2,3] \"job-runner\" 05:12 17-Feb-26\n```"),
|
||||||
|
("C1", "```\n[2,3] \"job-runner\" 05:13 17-Feb-26\n```"),
|
||||||
|
]
|
||||||
@@ -7,7 +7,8 @@ from lazy_enter.config import Config
|
|||||||
|
|
||||||
def test_config_defaults():
|
def test_config_defaults():
|
||||||
config = Config()
|
config = Config()
|
||||||
assert config.default_shell == "claude"
|
assert config.tmux_session_name == "claude"
|
||||||
|
assert config.codex_tmux_session_name == "codex"
|
||||||
assert config.pty_read_timeout == 5
|
assert config.pty_read_timeout == 5
|
||||||
assert config.output_buffer_interval == 2.0
|
assert config.output_buffer_interval == 2.0
|
||||||
assert config.max_message_length == 3000
|
assert config.max_message_length == 3000
|
||||||
|
|||||||
43
tests/test_output_filter.py
Normal file
43
tests/test_output_filter.py
Normal file
@@ -0,0 +1,43 @@
|
|||||||
|
"""output_filter 모듈 테스트."""
|
||||||
|
|
||||||
|
from lazy_enter.output_filter import clean_terminal_output
|
||||||
|
|
||||||
|
|
||||||
|
def test_clean_terminal_output_removes_ansi_sequences():
|
||||||
|
raw = "\x1b[?2004h\x1b[38;2;153;153;153mClaude Code\x1b[39m"
|
||||||
|
assert clean_terminal_output(raw) == "Claude Code"
|
||||||
|
|
||||||
|
|
||||||
|
def test_clean_terminal_output_removes_broken_csi_tokens():
|
||||||
|
raw = "[?2004h[38;2;153;153;153mhello[39m"
|
||||||
|
assert clean_terminal_output(raw) == "hello"
|
||||||
|
|
||||||
|
|
||||||
|
def test_clean_terminal_output_preserves_plain_text():
|
||||||
|
raw = "line1\nline2"
|
||||||
|
assert clean_terminal_output(raw) == "line1\nline2"
|
||||||
|
|
||||||
|
|
||||||
|
def test_clean_terminal_output_removes_broken_charset_tokens():
|
||||||
|
raw = "=(Bhello(B"
|
||||||
|
assert clean_terminal_output(raw) == "=hello"
|
||||||
|
|
||||||
|
|
||||||
|
def test_clean_terminal_output_filters_codex_tui_redraw_noise():
|
||||||
|
raw = """=(B╭─────────────────────────────────────────────╮(B
|
||||||
|
│ >_ OpenAI Codex (v0.101.0) │(B
|
||||||
|
│ model: gpt-5.3-codex /model to change │(B
|
||||||
|
│ directory: ~/repos/The-Ouroboros │(B
|
||||||
|
╰─────────────────────────────────────────────╯(B
|
||||||
|
Tip: New Codex is included in your plan for free
|
||||||
|
› Find and fix a bug in @filename
|
||||||
|
? for shortcuts
|
||||||
|
100% context left
|
||||||
|
Assistant: 작업을 시작합니다.
|
||||||
|
"""
|
||||||
|
assert clean_terminal_output(raw) == "Assistant: 작업을 시작합니다."
|
||||||
|
|
||||||
|
|
||||||
|
def test_clean_terminal_output_removes_box_empty_line_noise():
|
||||||
|
raw = "│ │\n› Explain this codebase\n"
|
||||||
|
assert clean_terminal_output(raw) == "› Explain this codebase"
|
||||||
@@ -1,22 +1,84 @@
|
|||||||
"""PtyManager 테스트."""
|
"""PtyManager 테스트."""
|
||||||
|
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
from collections.abc import Sequence
|
||||||
|
|
||||||
import pytest
|
import pytest
|
||||||
|
|
||||||
from lazy_enter.pty_manager import PtyManager
|
from lazy_enter.pty_manager import PtyManager
|
||||||
|
|
||||||
|
|
||||||
|
class FakeSpawn:
|
||||||
|
def __init__(self, *_args: object, **_kwargs: object) -> None:
|
||||||
|
self.before = ""
|
||||||
|
self._alive = True
|
||||||
|
|
||||||
|
def isalive(self) -> bool:
|
||||||
|
return self._alive
|
||||||
|
|
||||||
|
def sendline(self, _text: str) -> None:
|
||||||
|
return None
|
||||||
|
|
||||||
|
def expect(self, *_args: object, **_kwargs: object) -> None:
|
||||||
|
return None
|
||||||
|
|
||||||
|
def close(self, force: bool = False) -> None:
|
||||||
|
assert force is True
|
||||||
|
self._alive = False
|
||||||
|
|
||||||
|
|
||||||
def test_initial_state():
|
def test_initial_state():
|
||||||
pty = PtyManager("echo hello")
|
pty = PtyManager("claude")
|
||||||
assert not pty.is_alive
|
assert not pty.is_alive
|
||||||
|
|
||||||
|
|
||||||
def test_start_and_stop():
|
def test_start_raises_when_tmux_session_missing(monkeypatch: pytest.MonkeyPatch):
|
||||||
pty = PtyManager("cat")
|
def fake_run(
|
||||||
try:
|
cmd: Sequence[str],
|
||||||
pty.start()
|
check: bool,
|
||||||
except OSError as exc:
|
capture_output: bool,
|
||||||
pytest.skip(f"PTY unavailable in this environment: {exc}")
|
text: bool,
|
||||||
|
):
|
||||||
|
assert list(cmd) == ["tmux", "has-session", "-t", "claude"]
|
||||||
|
assert check is False
|
||||||
|
assert capture_output is True
|
||||||
|
assert text is True
|
||||||
|
|
||||||
|
class Result:
|
||||||
|
returncode = 1
|
||||||
|
|
||||||
|
return Result()
|
||||||
|
|
||||||
|
monkeypatch.setattr("lazy_enter.pty_manager.subprocess.run", fake_run)
|
||||||
|
|
||||||
|
pty = PtyManager("claude")
|
||||||
|
with pytest.raises(RuntimeError):
|
||||||
|
pty.start()
|
||||||
|
|
||||||
|
|
||||||
|
def test_start_and_stop_attach(monkeypatch: pytest.MonkeyPatch):
|
||||||
|
def fake_run(
|
||||||
|
_cmd: Sequence[str],
|
||||||
|
check: bool,
|
||||||
|
capture_output: bool,
|
||||||
|
text: bool,
|
||||||
|
):
|
||||||
|
assert check is False
|
||||||
|
assert capture_output is True
|
||||||
|
assert text is True
|
||||||
|
|
||||||
|
class Result:
|
||||||
|
returncode = 0
|
||||||
|
|
||||||
|
return Result()
|
||||||
|
|
||||||
|
monkeypatch.setattr("lazy_enter.pty_manager.subprocess.run", fake_run)
|
||||||
|
monkeypatch.setattr("lazy_enter.pty_manager.pexpect.spawn", FakeSpawn)
|
||||||
|
|
||||||
|
pty = PtyManager("claude")
|
||||||
|
pty.start()
|
||||||
assert pty.is_alive
|
assert pty.is_alive
|
||||||
|
|
||||||
pty.stop()
|
pty.stop()
|
||||||
assert not pty.is_alive
|
assert not pty.is_alive
|
||||||
|
|||||||
Reference in New Issue
Block a user