Separate Slack text input from Enter submission #11

Merged
jihoson merged 3 commits from fix/output-throttle into main 2026-02-17 06:04:03 +09:00
5 changed files with 114 additions and 14 deletions

View File

@@ -10,7 +10,7 @@
- Codex: `tmux new -s codex codex`
2. Slack에서 `/start-claude` 또는 `/start-codex` 실행
3. 브릿지가 기존 tmux 세션에 attach
4. Slack 채널 메시지가 CLI 입력으로 전달됨
4. Slack 채널 메시지가 CLI 입력으로 전달됨 (기본: 엔터 미포함)
5. CLI 출력이 Slack으로 다시 전송됨
6. `/stop-claude`로 브릿지 연결 해제 (tmux 세션은 유지)
@@ -81,7 +81,8 @@ python -m lazy_enter
실행 후 Slack의 허용된 채널에서:
- `/start-claude`, `/start-codex`: 기존 세션에 연결
- 일반 메시지 전송: 현재 연결된 CLI(Claude/Codex)로 입력 전달
- 일반 메시지 전송: 현재 연결된 CLI(Claude/Codex)로 입력 전달 (엔터 미포함)
- `!e`, `!enter` 전송: 엔터 키만 전달 (현재 프롬프트 제출)
- `/stop-claude`, `/stop-codex`: 브릿지 연결 해제 (세션 유지)
## 테스트 및 품질 점검

View File

@@ -18,6 +18,8 @@ logger = logging.getLogger(__name__)
class Bridge:
"""Slack ↔ CLI 프로세스 간의 중계기."""
ENTER_COMMANDS = {"!e", "!enter"}
def __init__(self, config: Config | None = None) -> None:
self.config = config or Config()
self.slack = SlackHandler(self.config)
@@ -39,22 +41,32 @@ class Bridge:
def _handle_message(self, text: str, channel: str) -> None:
"""Slack 메시지를 PTY 프로세스로 전달한다."""
if not self.pty or not self.pty.is_alive:
self.slack.send_message(channel, ":warning: 연결된 세션이 없습니다.")
return
if text.strip().lower() in self.ENTER_COMMANDS:
self.pty.send_enter()
self._last_sent_output = ""
self._last_sent_fingerprint = None
self._last_input_at = time.monotonic()
self._input_idle_reported = False
logger.info("엔터 입력 전달")
return
if self._is_blocked_input(text):
self.slack.send_message(
channel, ":no_entry: 차단된 명령 패턴이 감지되었습니다."
)
return
if self.pty and self.pty.is_alive:
self.pty.send(text)
# 입력 이후 출력은 동일 문자열이어도 한 번 더 전달한다.
self._last_sent_output = ""
self._last_sent_fingerprint = None
self._last_input_at = time.monotonic()
self._input_idle_reported = False
logger.info("입력 전달: %s", text)
else:
self.slack.send_message(channel, ":warning: 연결된 세션이 없습니다.")
logger.info("입력 전달(엔터 미포함): %s", text)
@staticmethod
def _is_blocked_input(text: str) -> bool:

View File

@@ -47,13 +47,20 @@ class PtyManager:
timeout=None,
)
def send(self, text: str) -> None:
def send(self, text: str, submit: bool = False) -> None:
"""프로세스에 텍스트 입력을 전달한다."""
if not self.is_alive:
raise RuntimeError("프로세스가 실행 중이 아닙니다.")
assert self._process is not None
logger.debug("입력 전송: %s", text)
if submit:
self._process.sendline(text)
return
self._process.send(text)
def send_enter(self) -> None:
"""엔터 키 입력만 전송한다."""
self.send("", submit=True)
def read_output(self, timeout: float = 5) -> str:
"""프로세스의 출력을 읽는다."""

View File

@@ -46,6 +46,7 @@ class FakePtyManager:
self.cli_name = cli_name
self._alive = False
self.sent_inputs: list[str] = []
self.enter_count = 0
FakePtyManager.instances.append(self)
@property
@@ -61,6 +62,9 @@ class FakePtyManager:
def send(self, text: str) -> None:
self.sent_inputs.append(text)
def send_enter(self) -> None:
self.enter_count += 1
def read_output(self, timeout: float = 5) -> str:
return ""
@@ -143,6 +147,45 @@ def test_handle_message_resets_last_sent_output_after_input(monkeypatch) -> None
assert bridge._last_sent_fingerprint is None
def test_handle_message_enter_command_sends_enter_only(monkeypatch) -> None:
FakePtyManager.instances.clear()
bridge = _make_bridge(monkeypatch)
bridge._handle_command("start", "codex", "C1")
pty = FakePtyManager.instances[-1]
bridge._handle_message("!enter", "C1")
assert pty.sent_inputs == []
assert pty.enter_count == 1
def test_handle_message_short_enter_alias_sends_enter_only(monkeypatch) -> None:
FakePtyManager.instances.clear()
bridge = _make_bridge(monkeypatch)
bridge._handle_command("start", "codex", "C1")
pty = FakePtyManager.instances[-1]
bridge._handle_message("!e", "C1")
assert pty.sent_inputs == []
assert pty.enter_count == 1
def test_handle_message_bang_is_plain_input(monkeypatch) -> None:
FakePtyManager.instances.clear()
bridge = _make_bridge(monkeypatch)
bridge._handle_command("start", "codex", "C1")
pty = FakePtyManager.instances[-1]
bridge._handle_message("!", "C1")
assert pty.sent_inputs == ["!"]
assert pty.enter_count == 0
def test_split_message_preserves_all_content(monkeypatch) -> None:
bridge = _make_bridge(monkeypatch)
chunks = bridge._split_message("line1\nline2\nline3", max_length=7)

View File

@@ -13,11 +13,18 @@ class FakeSpawn:
def __init__(self, *_args: object, **_kwargs: object) -> None:
self.before = ""
self._alive = True
self.sent: list[str] = []
self.sentline: list[str] = []
def isalive(self) -> bool:
return self._alive
def sendline(self, _text: str) -> None:
self.sentline.append(_text)
return None
def send(self, _text: str) -> None:
self.sent.append(_text)
return None
def expect(self, *_args: object, **_kwargs: object) -> None:
@@ -82,3 +89,33 @@ def test_start_and_stop_attach(monkeypatch: pytest.MonkeyPatch):
pty.stop()
assert not pty.is_alive
def test_send_and_send_enter_are_separated(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._process is not None
pty.send("status")
pty.send_enter()
assert pty._process.sent == ["status"]
assert pty._process.sentline == [""]