Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 21 additions & 5 deletions src/ccbot/session_monitor.py
Original file line number Diff line number Diff line change
Expand Up @@ -272,18 +272,34 @@ async def check_for_updates(self, active_session_ids: set[str]) -> list[NewMessa
Reads from last byte offset. Emits both intermediate
(stop_reason=null) and complete messages.

For already-tracked sessions, uses stored file paths (fast path).
Only runs the expensive scan_projects() when there are new
session IDs not yet tracked.

Args:
active_session_ids: Set of session IDs currently in session_map
"""
new_messages = []

# Scan projects to get available session files
sessions = await self.scan_projects()
# Fast path: build session list from already-tracked sessions
tracked_ids = set(self.state.tracked_sessions.keys())
sessions: list[SessionInfo] = []
for sid in active_session_ids & tracked_ids:
tracked = self.state.get_session(sid)
if tracked and tracked.file_path:
fp = Path(tracked.file_path)
if fp.exists():
sessions.append(SessionInfo(session_id=sid, file_path=fp))

# Only scan projects if there are untracked active sessions
untracked = active_session_ids - tracked_ids
if untracked:
scanned = await self.scan_projects()
for si in scanned:
if si.session_id in untracked:
sessions.append(si)

# Only process sessions that are in session_map
for session_info in sessions:
if session_info.session_id not in active_session_ids:
continue
try:
tracked = self.state.get_session(session_info.session_id)

Expand Down
18 changes: 17 additions & 1 deletion src/ccbot/tmux_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

import asyncio
import logging
import time
from dataclasses import dataclass
from pathlib import Path

Expand All @@ -38,6 +39,9 @@ class TmuxWindow:
class TmuxManager:
"""Manages tmux windows for Claude Code sessions."""

# TTL for cached list_windows results (seconds)
_CACHE_TTL = 1.0

def __init__(self, session_name: str | None = None):
"""Initialize tmux manager.

Expand All @@ -46,6 +50,8 @@ def __init__(self, session_name: str | None = None):
"""
self.session_name = session_name or config.tmux_session_name
self._server: libtmux.Server | None = None
self._cached_windows: list[TmuxWindow] | None = None
self._cache_time: float = 0.0

@property
def server(self) -> libtmux.Server:
Expand Down Expand Up @@ -95,9 +101,16 @@ def _scrub_session_env(session: libtmux.Session) -> None:
async def list_windows(self) -> list[TmuxWindow]:
"""List all windows in the session with their working directories.

Results are cached for _CACHE_TTL seconds to avoid redundant
libtmux Server queries from concurrent callers (status poll,
session monitor, message queue).

Returns:
List of TmuxWindow with window info and cwd
"""
now = time.monotonic()
if self._cached_windows is not None and (now - self._cache_time) < self._CACHE_TTL:
return self._cached_windows

def _sync_list_windows() -> list[TmuxWindow]:
windows = []
Expand Down Expand Up @@ -135,7 +148,10 @@ def _sync_list_windows() -> list[TmuxWindow]:

return windows

return await asyncio.to_thread(_sync_list_windows)
windows = await asyncio.to_thread(_sync_list_windows)
self._cached_windows = windows
self._cache_time = time.monotonic()
return windows

async def find_window_by_name(self, window_name: str) -> TmuxWindow | None:
"""Find a window by its name.
Expand Down
Loading