Compare commits
5 Commits
fix/slack-
...
fix/output
| Author | SHA1 | Date | |
|---|---|---|---|
| 6049ba7a8a | |||
| b3ce2622e3 | |||
| 271376a7da | |||
| 51e63a0f85 | |||
| 3effedf07d |
@@ -13,6 +13,8 @@ PTY_READ_TIMEOUT=5
|
|||||||
|
|
||||||
# 출력 버퍼 설정
|
# 출력 버퍼 설정
|
||||||
OUTPUT_BUFFER_INTERVAL=2.0
|
OUTPUT_BUFFER_INTERVAL=2.0
|
||||||
|
OUTPUT_SETTLE_SECONDS=4.0
|
||||||
|
OUTPUT_FLUSH_INTERVAL_SECONDS=15.0
|
||||||
MAX_MESSAGE_LENGTH=3000
|
MAX_MESSAGE_LENGTH=3000
|
||||||
|
|
||||||
# 상태 보고 / 재연결 설정
|
# 상태 보고 / 재연결 설정
|
||||||
|
|||||||
@@ -10,7 +10,7 @@
|
|||||||
- Codex: `tmux new -s codex codex`
|
- Codex: `tmux new -s codex codex`
|
||||||
2. Slack에서 `/start-claude` 또는 `/start-codex` 실행
|
2. Slack에서 `/start-claude` 또는 `/start-codex` 실행
|
||||||
3. 브릿지가 기존 tmux 세션에 attach
|
3. 브릿지가 기존 tmux 세션에 attach
|
||||||
4. Slack 채널 메시지가 CLI 입력으로 전달됨
|
4. Slack 채널 메시지가 CLI 입력으로 전달됨 (기본: 엔터 미포함)
|
||||||
5. CLI 출력이 Slack으로 다시 전송됨
|
5. CLI 출력이 Slack으로 다시 전송됨
|
||||||
6. `/stop-claude`로 브릿지 연결 해제 (tmux 세션은 유지)
|
6. `/stop-claude`로 브릿지 연결 해제 (tmux 세션은 유지)
|
||||||
|
|
||||||
@@ -41,6 +41,8 @@ cp .env.example .env
|
|||||||
- `CODEX_TMUX_SESSION_NAME` (기본: `codex`, `/start-codex` 대상)
|
- `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`)
|
||||||
|
- `OUTPUT_SETTLE_SECONDS` (기본: `4.0`, 출력이 잠잠해진 뒤 전송 대기 시간)
|
||||||
|
- `OUTPUT_FLUSH_INTERVAL_SECONDS` (기본: `15.0`, 출력이 계속 이어질 때 강제 전송 주기)
|
||||||
- `MAX_MESSAGE_LENGTH` (기본: `3000`)
|
- `MAX_MESSAGE_LENGTH` (기본: `3000`)
|
||||||
- `RECONNECT_DELAY_SECONDS` (기본: `5.0`, Socket Mode 재연결 대기 시간)
|
- `RECONNECT_DELAY_SECONDS` (기본: `5.0`, Socket Mode 재연결 대기 시간)
|
||||||
- `OUTPUT_IDLE_REPORT_SECONDS` (기본: `120`, 출력 정지 보고 임계값)
|
- `OUTPUT_IDLE_REPORT_SECONDS` (기본: `120`, 출력 정지 보고 임계값)
|
||||||
@@ -79,7 +81,8 @@ python -m lazy_enter
|
|||||||
|
|
||||||
실행 후 Slack의 허용된 채널에서:
|
실행 후 Slack의 허용된 채널에서:
|
||||||
- `/start-claude`, `/start-codex`: 기존 세션에 연결
|
- `/start-claude`, `/start-codex`: 기존 세션에 연결
|
||||||
- 일반 메시지 전송: 현재 연결된 CLI(Claude/Codex)로 입력 전달
|
- 일반 메시지 전송: 현재 연결된 CLI(Claude/Codex)로 입력만 전달 (엔터 미포함)
|
||||||
|
- `!e`, `!enter` 전송: 엔터 키만 전달 (현재 프롬프트 제출)
|
||||||
- `/stop-claude`, `/stop-codex`: 브릿지 연결 해제 (세션 유지)
|
- `/stop-claude`, `/stop-codex`: 브릿지 연결 해제 (세션 유지)
|
||||||
|
|
||||||
## 테스트 및 품질 점검
|
## 테스트 및 품질 점검
|
||||||
|
|||||||
@@ -18,6 +18,8 @@ logger = logging.getLogger(__name__)
|
|||||||
class Bridge:
|
class Bridge:
|
||||||
"""Slack ↔ CLI 프로세스 간의 중계기."""
|
"""Slack ↔ CLI 프로세스 간의 중계기."""
|
||||||
|
|
||||||
|
ENTER_COMMANDS = {"!e", "!enter"}
|
||||||
|
|
||||||
def __init__(self, config: Config | None = None) -> None:
|
def __init__(self, config: Config | None = None) -> None:
|
||||||
self.config = config or Config()
|
self.config = config or Config()
|
||||||
self.slack = SlackHandler(self.config)
|
self.slack = SlackHandler(self.config)
|
||||||
@@ -30,6 +32,7 @@ class Bridge:
|
|||||||
self._last_sent_fingerprint: str | None = None
|
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._output_buffer_started_at: float | None = None
|
||||||
self._input_idle_reported = False
|
self._input_idle_reported = False
|
||||||
self._output_idle_reported = False
|
self._output_idle_reported = False
|
||||||
|
|
||||||
@@ -38,22 +41,32 @@ class Bridge:
|
|||||||
|
|
||||||
def _handle_message(self, text: str, channel: str) -> None:
|
def _handle_message(self, text: str, channel: str) -> None:
|
||||||
"""Slack 메시지를 PTY 프로세스로 전달한다."""
|
"""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):
|
if self._is_blocked_input(text):
|
||||||
self.slack.send_message(
|
self.slack.send_message(
|
||||||
channel, ":no_entry: 차단된 명령 패턴이 감지되었습니다."
|
channel, ":no_entry: 차단된 명령 패턴이 감지되었습니다."
|
||||||
)
|
)
|
||||||
return
|
return
|
||||||
|
|
||||||
if self.pty and self.pty.is_alive:
|
self.pty.send(text)
|
||||||
self.pty.send(text)
|
# 입력 이후 출력은 동일 문자열이어도 한 번 더 전달한다.
|
||||||
# 입력 이후 출력은 동일 문자열이어도 한 번 더 전달한다.
|
self._last_sent_output = ""
|
||||||
self._last_sent_output = ""
|
self._last_sent_fingerprint = None
|
||||||
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:
|
|
||||||
self.slack.send_message(channel, ":warning: 연결된 세션이 없습니다.")
|
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def _is_blocked_input(text: str) -> bool:
|
def _is_blocked_input(text: str) -> bool:
|
||||||
@@ -118,6 +131,7 @@ class Bridge:
|
|||||||
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()
|
||||||
|
self._output_buffer_started_at = None
|
||||||
self._input_idle_reported = False
|
self._input_idle_reported = False
|
||||||
self._output_idle_reported = False
|
self._output_idle_reported = False
|
||||||
self._output_thread = threading.Thread(target=self._poll_output, daemon=True)
|
self._output_thread = threading.Thread(target=self._poll_output, daemon=True)
|
||||||
@@ -137,6 +151,7 @@ class Bridge:
|
|||||||
self._active_target = None
|
self._active_target = None
|
||||||
self._last_sent_output = ""
|
self._last_sent_output = ""
|
||||||
self._last_sent_fingerprint = None
|
self._last_sent_fingerprint = None
|
||||||
|
self._output_buffer_started_at = None
|
||||||
self.slack.send_message(channel, ":electric_plug: 세션 연결이 해제되었습니다.")
|
self.slack.send_message(channel, ":electric_plug: 세션 연결이 해제되었습니다.")
|
||||||
|
|
||||||
def _poll_output(self) -> None:
|
def _poll_output(self) -> None:
|
||||||
@@ -144,17 +159,27 @@ class Bridge:
|
|||||||
buffer = ""
|
buffer = ""
|
||||||
while self._running and self.pty and self.pty.is_alive:
|
while self._running and self.pty and self.pty.is_alive:
|
||||||
now = time.monotonic()
|
now = time.monotonic()
|
||||||
output = self.pty.read_output(timeout=self.config.pty_read_timeout)
|
if buffer and self._should_flush_output_buffer(now):
|
||||||
|
self._send_output_chunks(buffer)
|
||||||
|
buffer = ""
|
||||||
|
self._output_buffer_started_at = None
|
||||||
|
|
||||||
|
read_timeout = self._next_read_timeout(now, has_buffer=bool(buffer))
|
||||||
|
output = self.pty.read_output(timeout=read_timeout)
|
||||||
|
now = time.monotonic()
|
||||||
if output:
|
if output:
|
||||||
cleaned = clean_terminal_output(output)
|
cleaned = clean_terminal_output(output)
|
||||||
if cleaned:
|
if cleaned:
|
||||||
buffer += f"{cleaned}\n"
|
buffer += f"{cleaned}\n"
|
||||||
self._last_output_at = now
|
self._last_output_at = now
|
||||||
|
if self._output_buffer_started_at is None:
|
||||||
|
self._output_buffer_started_at = now
|
||||||
self._output_idle_reported = False
|
self._output_idle_reported = False
|
||||||
|
|
||||||
if buffer:
|
if buffer and self._should_flush_output_buffer(now):
|
||||||
self._send_output_chunks(buffer)
|
self._send_output_chunks(buffer)
|
||||||
buffer = ""
|
buffer = ""
|
||||||
|
self._output_buffer_started_at = None
|
||||||
|
|
||||||
output_idle = now - self._last_output_at
|
output_idle = now - self._last_output_at
|
||||||
if (
|
if (
|
||||||
@@ -186,11 +211,51 @@ class Bridge:
|
|||||||
time.sleep(self.config.output_buffer_interval)
|
time.sleep(self.config.output_buffer_interval)
|
||||||
|
|
||||||
if not self._running:
|
if not self._running:
|
||||||
|
self._output_buffer_started_at = None
|
||||||
return
|
return
|
||||||
|
|
||||||
|
if buffer:
|
||||||
|
self._send_output_chunks(buffer)
|
||||||
|
self._output_buffer_started_at = None
|
||||||
|
|
||||||
# attach 프로세스가 예기치 않게 종료된 경우
|
# attach 프로세스가 예기치 않게 종료된 경우
|
||||||
self.slack.send_message(self._channel, ":warning: 세션 연결이 종료되었습니다.")
|
self.slack.send_message(self._channel, ":warning: 세션 연결이 종료되었습니다.")
|
||||||
|
|
||||||
|
def _next_read_timeout(self, now: float, has_buffer: bool) -> float:
|
||||||
|
"""다음 PTY 읽기 타임아웃을 계산한다."""
|
||||||
|
base_timeout = max(0.0, float(self.config.pty_read_timeout))
|
||||||
|
if not has_buffer:
|
||||||
|
return base_timeout
|
||||||
|
|
||||||
|
deadline = self._next_output_flush_deadline()
|
||||||
|
if deadline is None:
|
||||||
|
return base_timeout
|
||||||
|
|
||||||
|
remaining = max(0.0, deadline - now)
|
||||||
|
return min(base_timeout, remaining)
|
||||||
|
|
||||||
|
def _next_output_flush_deadline(self) -> float | None:
|
||||||
|
"""버퍼 flush의 가장 이른 데드라인을 반환한다."""
|
||||||
|
deadlines: list[float] = []
|
||||||
|
settle_seconds = max(0.0, self.config.output_settle_seconds)
|
||||||
|
if settle_seconds > 0:
|
||||||
|
deadlines.append(self._last_output_at + settle_seconds)
|
||||||
|
|
||||||
|
flush_interval_seconds = max(0.0, self.config.output_flush_interval_seconds)
|
||||||
|
if self._output_buffer_started_at is not None:
|
||||||
|
if flush_interval_seconds == 0:
|
||||||
|
return self._output_buffer_started_at
|
||||||
|
deadlines.append(self._output_buffer_started_at + flush_interval_seconds)
|
||||||
|
|
||||||
|
if not deadlines:
|
||||||
|
return None
|
||||||
|
return min(deadlines)
|
||||||
|
|
||||||
|
def _should_flush_output_buffer(self, now: float) -> bool:
|
||||||
|
"""버퍼를 Slack으로 전송할 시점을 계산한다."""
|
||||||
|
deadline = self._next_output_flush_deadline()
|
||||||
|
return deadline is not None and now >= deadline
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def _split_message(text: str, max_length: int) -> list[str]:
|
def _split_message(text: str, max_length: int) -> list[str]:
|
||||||
"""긴 텍스트를 메시지 길이 제한에 맞게 분할한다."""
|
"""긴 텍스트를 메시지 길이 제한에 맞게 분할한다."""
|
||||||
|
|||||||
@@ -25,6 +25,10 @@ class Config:
|
|||||||
|
|
||||||
# Buffer
|
# Buffer
|
||||||
output_buffer_interval: float = float(os.getenv("OUTPUT_BUFFER_INTERVAL", "2.0"))
|
output_buffer_interval: float = float(os.getenv("OUTPUT_BUFFER_INTERVAL", "2.0"))
|
||||||
|
output_settle_seconds: float = float(os.getenv("OUTPUT_SETTLE_SECONDS", "4.0"))
|
||||||
|
output_flush_interval_seconds: float = float(
|
||||||
|
os.getenv("OUTPUT_FLUSH_INTERVAL_SECONDS", "15.0")
|
||||||
|
)
|
||||||
max_message_length: int = int(os.getenv("MAX_MESSAGE_LENGTH", "3000"))
|
max_message_length: int = int(os.getenv("MAX_MESSAGE_LENGTH", "3000"))
|
||||||
|
|
||||||
# Status reporting / reconnect
|
# Status reporting / reconnect
|
||||||
|
|||||||
@@ -47,15 +47,22 @@ class PtyManager:
|
|||||||
timeout=None,
|
timeout=None,
|
||||||
)
|
)
|
||||||
|
|
||||||
def send(self, text: str) -> None:
|
def send(self, text: str, submit: bool = False) -> None:
|
||||||
"""프로세스에 텍스트 입력을 전달한다."""
|
"""프로세스에 텍스트 입력을 전달한다."""
|
||||||
if not self.is_alive:
|
if not self.is_alive:
|
||||||
raise RuntimeError("프로세스가 실행 중이 아닙니다.")
|
raise RuntimeError("프로세스가 실행 중이 아닙니다.")
|
||||||
assert self._process is not None
|
assert self._process is not None
|
||||||
logger.debug("입력 전송: %s", text)
|
logger.debug("입력 전송: %s", text)
|
||||||
self._process.sendline(text)
|
if submit:
|
||||||
|
self._process.sendline(text)
|
||||||
|
return
|
||||||
|
self._process.send(text)
|
||||||
|
|
||||||
def read_output(self, timeout: int = 5) -> str:
|
def send_enter(self) -> None:
|
||||||
|
"""엔터 키 입력만 전송한다."""
|
||||||
|
self.send("", submit=True)
|
||||||
|
|
||||||
|
def read_output(self, timeout: float = 5) -> str:
|
||||||
"""프로세스의 출력을 읽는다."""
|
"""프로세스의 출력을 읽는다."""
|
||||||
if not self.is_alive:
|
if not self.is_alive:
|
||||||
raise RuntimeError("프로세스가 실행 중이 아닙니다.")
|
raise RuntimeError("프로세스가 실행 중이 아닙니다.")
|
||||||
|
|||||||
@@ -46,6 +46,7 @@ class FakePtyManager:
|
|||||||
self.cli_name = cli_name
|
self.cli_name = cli_name
|
||||||
self._alive = False
|
self._alive = False
|
||||||
self.sent_inputs: list[str] = []
|
self.sent_inputs: list[str] = []
|
||||||
|
self.enter_count = 0
|
||||||
FakePtyManager.instances.append(self)
|
FakePtyManager.instances.append(self)
|
||||||
|
|
||||||
@property
|
@property
|
||||||
@@ -61,7 +62,10 @@ class FakePtyManager:
|
|||||||
def send(self, text: str) -> None:
|
def send(self, text: str) -> None:
|
||||||
self.sent_inputs.append(text)
|
self.sent_inputs.append(text)
|
||||||
|
|
||||||
def read_output(self, timeout: int = 5) -> str:
|
def send_enter(self) -> None:
|
||||||
|
self.enter_count += 1
|
||||||
|
|
||||||
|
def read_output(self, timeout: float = 5) -> str:
|
||||||
return ""
|
return ""
|
||||||
|
|
||||||
|
|
||||||
@@ -143,6 +147,45 @@ def test_handle_message_resets_last_sent_output_after_input(monkeypatch) -> None
|
|||||||
assert bridge._last_sent_fingerprint is 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:
|
def test_split_message_preserves_all_content(monkeypatch) -> None:
|
||||||
bridge = _make_bridge(monkeypatch)
|
bridge = _make_bridge(monkeypatch)
|
||||||
chunks = bridge._split_message("line1\nline2\nline3", max_length=7)
|
chunks = bridge._split_message("line1\nline2\nline3", max_length=7)
|
||||||
@@ -297,3 +340,105 @@ def test_send_output_chunks_keeps_non_tmux_status_like_lines(monkeypatch) -> Non
|
|||||||
("C1", "```\n[2,3] \"job-runner\" 05:12 17-Feb-26\n```"),
|
("C1", "```\n[2,3] \"job-runner\" 05:12 17-Feb-26\n```"),
|
||||||
("C1", "```\n[2,3] \"job-runner\" 05:13 17-Feb-26\n```"),
|
("C1", "```\n[2,3] \"job-runner\" 05:13 17-Feb-26\n```"),
|
||||||
]
|
]
|
||||||
|
|
||||||
|
|
||||||
|
def test_should_flush_output_buffer_when_settled(monkeypatch) -> None:
|
||||||
|
bridge = _make_bridge(monkeypatch)
|
||||||
|
bridge.config.output_settle_seconds = 4.0
|
||||||
|
bridge.config.output_flush_interval_seconds = 15.0
|
||||||
|
bridge._last_output_at = 10.0
|
||||||
|
bridge._output_buffer_started_at = 2.0
|
||||||
|
|
||||||
|
assert bridge._should_flush_output_buffer(14.1) is True
|
||||||
|
|
||||||
|
|
||||||
|
def test_should_flush_output_buffer_when_flush_interval_elapsed(monkeypatch) -> None:
|
||||||
|
bridge = _make_bridge(monkeypatch)
|
||||||
|
bridge.config.output_settle_seconds = 4.0
|
||||||
|
bridge.config.output_flush_interval_seconds = 15.0
|
||||||
|
bridge._last_output_at = 20.0
|
||||||
|
bridge._output_buffer_started_at = 2.0
|
||||||
|
|
||||||
|
assert bridge._should_flush_output_buffer(17.1) is True
|
||||||
|
|
||||||
|
|
||||||
|
def test_should_flush_output_buffer_false_during_active_stream(monkeypatch) -> None:
|
||||||
|
bridge = _make_bridge(monkeypatch)
|
||||||
|
bridge.config.output_settle_seconds = 4.0
|
||||||
|
bridge.config.output_flush_interval_seconds = 15.0
|
||||||
|
bridge._last_output_at = 19.0
|
||||||
|
bridge._output_buffer_started_at = 10.0
|
||||||
|
|
||||||
|
assert bridge._should_flush_output_buffer(20.0) is False
|
||||||
|
|
||||||
|
|
||||||
|
def test_poll_output_skips_final_flush_after_intentional_stop(monkeypatch) -> None:
|
||||||
|
bridge = _make_bridge(monkeypatch)
|
||||||
|
bridge._channel = "C1"
|
||||||
|
bridge.config.output_settle_seconds = 9999.0
|
||||||
|
bridge.config.output_flush_interval_seconds = 9999.0
|
||||||
|
bridge.config.output_buffer_interval = 0.0
|
||||||
|
|
||||||
|
pty = FakePtyManager("codex-room", cli_name="codex")
|
||||||
|
pty._alive = True
|
||||||
|
bridge.pty = pty
|
||||||
|
bridge._running = True
|
||||||
|
|
||||||
|
sent_buffers: list[str] = []
|
||||||
|
monkeypatch.setattr(bridge, "_send_output_chunks", sent_buffers.append)
|
||||||
|
|
||||||
|
def _read_output(timeout: float = 5) -> str:
|
||||||
|
bridge._running = False
|
||||||
|
return "planning update"
|
||||||
|
|
||||||
|
monkeypatch.setattr(pty, "read_output", _read_output)
|
||||||
|
bridge._poll_output()
|
||||||
|
|
||||||
|
assert sent_buffers == []
|
||||||
|
|
||||||
|
|
||||||
|
def test_next_read_timeout_is_capped_by_flush_deadline(monkeypatch) -> None:
|
||||||
|
bridge = _make_bridge(monkeypatch)
|
||||||
|
bridge.config.pty_read_timeout = 5
|
||||||
|
bridge.config.output_settle_seconds = 4.0
|
||||||
|
bridge.config.output_flush_interval_seconds = 15.0
|
||||||
|
bridge._last_output_at = 100.0
|
||||||
|
bridge._output_buffer_started_at = 95.0
|
||||||
|
|
||||||
|
timeout = bridge._next_read_timeout(103.6, has_buffer=True)
|
||||||
|
assert 0.39 <= timeout <= 0.41
|
||||||
|
|
||||||
|
|
||||||
|
def test_poll_output_uses_shorter_timeout_near_settle_deadline(monkeypatch) -> None:
|
||||||
|
bridge = _make_bridge(monkeypatch)
|
||||||
|
bridge._channel = "C1"
|
||||||
|
bridge.config.pty_read_timeout = 5
|
||||||
|
bridge.config.output_settle_seconds = 4.0
|
||||||
|
bridge.config.output_flush_interval_seconds = 15.0
|
||||||
|
bridge.config.output_buffer_interval = 0.0
|
||||||
|
bridge.config.output_idle_report_seconds = 0
|
||||||
|
bridge.config.input_idle_report_seconds = 0
|
||||||
|
|
||||||
|
pty = FakePtyManager("codex-room", cli_name="codex")
|
||||||
|
pty._alive = True
|
||||||
|
bridge.pty = pty
|
||||||
|
bridge._running = True
|
||||||
|
|
||||||
|
observed_timeouts: list[float] = []
|
||||||
|
call_count = 0
|
||||||
|
|
||||||
|
def _read_output(timeout: float = 5) -> str:
|
||||||
|
nonlocal call_count
|
||||||
|
observed_timeouts.append(timeout)
|
||||||
|
if call_count == 0:
|
||||||
|
call_count += 1
|
||||||
|
return "first chunk"
|
||||||
|
bridge._running = False
|
||||||
|
return ""
|
||||||
|
|
||||||
|
monkeypatch.setattr(pty, "read_output", _read_output)
|
||||||
|
bridge._poll_output()
|
||||||
|
|
||||||
|
assert len(observed_timeouts) == 2
|
||||||
|
assert observed_timeouts[0] == 5
|
||||||
|
assert 0.0 <= observed_timeouts[1] < 5
|
||||||
|
|||||||
@@ -11,6 +11,8 @@ def test_config_defaults():
|
|||||||
assert config.codex_tmux_session_name == "codex"
|
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.output_settle_seconds == 4.0
|
||||||
|
assert config.output_flush_interval_seconds == 15.0
|
||||||
assert config.max_message_length == 3000
|
assert config.max_message_length == 3000
|
||||||
assert config.reconnect_delay_seconds == 5.0
|
assert config.reconnect_delay_seconds == 5.0
|
||||||
assert config.output_idle_report_seconds == 120
|
assert config.output_idle_report_seconds == 120
|
||||||
|
|||||||
@@ -13,11 +13,18 @@ class FakeSpawn:
|
|||||||
def __init__(self, *_args: object, **_kwargs: object) -> None:
|
def __init__(self, *_args: object, **_kwargs: object) -> None:
|
||||||
self.before = ""
|
self.before = ""
|
||||||
self._alive = True
|
self._alive = True
|
||||||
|
self.sent: list[str] = []
|
||||||
|
self.sentline: list[str] = []
|
||||||
|
|
||||||
def isalive(self) -> bool:
|
def isalive(self) -> bool:
|
||||||
return self._alive
|
return self._alive
|
||||||
|
|
||||||
def sendline(self, _text: str) -> None:
|
def sendline(self, _text: str) -> None:
|
||||||
|
self.sentline.append(_text)
|
||||||
|
return None
|
||||||
|
|
||||||
|
def send(self, _text: str) -> None:
|
||||||
|
self.sent.append(_text)
|
||||||
return None
|
return None
|
||||||
|
|
||||||
def expect(self, *_args: object, **_kwargs: object) -> None:
|
def expect(self, *_args: object, **_kwargs: object) -> None:
|
||||||
@@ -82,3 +89,33 @@ def test_start_and_stop_attach(monkeypatch: pytest.MonkeyPatch):
|
|||||||
|
|
||||||
pty.stop()
|
pty.stop()
|
||||||
assert not pty.is_alive
|
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 == [""]
|
||||||
|
|||||||
Reference in New Issue
Block a user