Skip to content

Use Claude Code, Codex, VSCode, Pi, and OpenCode (and 6 other harnesses) for free (1.3B+ free tokens) from your terminal, app, IDE, or phone, and now from the browser with native browser sessions (multi-harness + multi-model) like OpenClaw (voice supported + ToS friendly)

Python

cleaned up legacy code and added new tests

Alishahryar18222c450efb3
diff --git a/providers/claude_cli.py b/providers/claude_cli.py
deleted file mode 100644
index da8f8b45..00000000
--- a/providers/claude_cli.py
+++ /dev/null
@@ -1,321 +0,0 @@
-import asyncio
-import os
-import json
-import logging
-from typing import AsyncGenerator, Optional, Dict, List
-
-logger = logging.getLogger(__name__)
-
-
-class CLIParser:
- """Helper to structure raw CLI events."""
-
- @staticmethod
- def parse_event(event: Dict) -> Optional[Dict]:
- if not isinstance(event, dict):
- return None
-
- etype = event.get("type")
-
- # 1. Handle full messages (assistant or result)
- msg_obj = None
- if etype == "assistant":
- msg_obj = event.get("message")
- elif etype == "result":
- # Safely get message from result which might be a list or dict
- res = event.get("result")
- logger.debug(f"Parsing result event: res type={type(res)}, keys={res.keys() if isinstance(res, dict) else 'N/A'}")
- if isinstance(res, dict):
- msg_obj = res.get("message")
- if not msg_obj:
- msg_obj = event.get("message")
- logger.debug(f"Result msg_obj: {msg_obj is not None}, type={type(msg_obj) if msg_obj else 'None'}")
-
- if msg_obj and isinstance(msg_obj, dict):
- content = msg_obj.get("content", [])
- if isinstance(content, list):
- parts = []
- thinking_parts = []
- tool_calls = []
- for c in content:
- if not isinstance(c, dict):
- continue
- ctype = c.get("type")
- if ctype == "text":
- parts.append(c.get("text", ""))
- elif ctype == "thinking":
- thinking_parts.append(c.get("thinking", ""))
- elif ctype == "tool_use":
- tool_calls.append(c)
-
- if tool_calls:
- # Check for subagents (Task tool)
- subagents = [
- t.get("input", {}).get("description", "Subagent")
- for t in tool_calls
- if t.get("name") == "Task"
- ]
- if subagents:
- return {"type": "subagent_start", "tasks": subagents}
- return {"type": "tool_start", "tools": tool_calls}
-
- # Return combined result if we have content
- result = {}
- if thinking_parts:
- result["thinking"] = "\n".join(thinking_parts)
- if parts:
- result["text"] = "".join(parts)
- if result:
- result["type"] = "content"
- return result
-
- # 2. Handle streaming deltas
- if etype == "content_block_delta":
- delta = event.get("delta", {})
- if not isinstance(delta, dict):
- return None
- if delta.get("type") == "text_delta":
- return {"type": "content", "text": delta.get("text", "")}
- if delta.get("type") == "thinking_delta":
- return {"type": "thinking", "text": delta.get("thinking", "")}
-
- # 3. Handle tool usage start
- if etype == "content_block_start":
- block = event.get("content_block", {})
- if isinstance(block, dict) and block.get("type") == "tool_use":
- if block.get("name") == "Task":
- desc = block.get("input", {}).get("description", "Subagent")
- return {"type": "subagent_start", "tasks": [desc]}
- return {"type": "tool_start", "tools": [block]}
-
- # 4. Handle errors and exit
- if etype == "error":
- err = event.get("error")
- msg = err.get("message") if isinstance(err, dict) else str(err)
- return {"type": "error", "message": msg}
- elif etype == "exit":
- return {
- "type": "complete",
- "status": "success" if event.get("code") == 0 else "failed",
- }
- return None
-
-
-class CLISession:
- """Manages a single persistent Claude Code CLI subprocess."""
-
- def __init__(
- self,
- workspace_path: str,
- api_url: str,
- allowed_dirs: Optional[List[str]] = None,
- ):
- self.workspace = os.path.normpath(os.path.abspath(workspace_path))
- self.api_url = api_url
- self.allowed_dirs = [os.path.normpath(d) for d in (allowed_dirs or [])]
- self.process: Optional[asyncio.subprocess.Process] = None
- self.current_session_id: Optional[str] = None
- self._is_busy = False
- # Global lock to prevent concurrent subprocess access (fixes readuntil race condition)
- self._cli_lock = asyncio.Lock()
-
- @property
- def is_busy(self) -> bool:
- """Check if a task is currently running."""
- return self._is_busy
-
- async def start_task(
- self, prompt: str, session_id: Optional[str] = None
- ) -> AsyncGenerator[dict, None]:
- """
- Start a new task or continue an existing session.
-
- Args:
- prompt: The user's message/prompt
- session_id: Optional session ID to resume. If provided, uses --resume flag.
-
- Yields:
- Event dictionaries from the CLI, including a special 'session_info' event
- with the session ID when available.
- """
- # Acquire lock to prevent concurrent CLI access (fixes readuntil race condition)
- async with self._cli_lock:
- self._is_busy = True
- env = os.environ.copy()
-
- # Ensure we have a dummy key if none exists, as CLI often requires it
- if "ANTHROPIC_API_KEY" not in env:
- env["ANTHROPIC_API_KEY"] = "sk-placeholder-key-for-proxy"
-
- env["ANTHROPIC_API_URL"] = self.api_url
- if self.api_url.endswith("/v1"):
- env["ANTHROPIC_BASE_URL"] = self.api_url[:-3]
- else:
- env["ANTHROPIC_BASE_URL"] = self.api_url
-
- # Force non-interactive non-TTY
- env["TERM"] = "dumb"
- env["PYTHONIOENCODING"] = "utf-8"
-
- # Build command based on whether resuming or starting new
- # Important: only use REAL session IDs for --resume, not our internal 'pending_' IDs
- if session_id and not session_id.startswith("pending_"):
- # Resume existing session
- cmd = [
- "claude",
- "--resume", session_id,
- "-p", prompt,
- "--output-format", "stream-json",
- "--dangerously-skip-permissions",
- "--verbose",
- ]
- logger.info(f"Resuming Claude session {session_id} with prompt: {prompt[:50]}...")
- else:
- # Start new session
- cmd = [
- "claude",
- "-p", prompt,
- "--output-format", "stream-json",
- "--dangerously-skip-permissions",
- "--verbose",
- ]
- logger.info(f"Starting new Claude session with prompt: {prompt[:50]}...")
-
- if self.allowed_dirs:
- for d in self.allowed_dirs:
- cmd.extend(["--add-dir", d])
-
- logger.info(f"Launching Claude CLI in {self.workspace}")
-
- try:
- self.process = await asyncio.create_subprocess_exec(
- *cmd,
- stdout=asyncio.subprocess.PIPE,
- stderr=asyncio.subprocess.PIPE,
- cwd=self.workspace,
- env=env,
- )
-
- if not self.process or not self.process.stdout:
- logger.error("Claude CLI process failed to start or stdout not captured.")
- yield {"type": "exit", "code": 1}
- return
-
- session_id_extracted = False
- buffer = bytearray()
-
- while True:
- # Read in chunks to handle extremely long lines (bypass 64KB limit)
- chunk = await self.process.stdout.read(65536)
- if not chunk:
- # End of stream, process any remaining data in buffer
- if buffer:
- line_str = buffer.decode("utf-8", errors="replace").strip()
- if line_str:
- async for result_event in self._handle_line_gen(line_str, session_id_extracted):
- if result_event.get("type") == "session_info":
- session_id_extracted = True
- yield result_event
- break
-
- buffer.extend(chunk)
-
- # Split buffer by newlines and process complete lines
- while True:
- newline_pos = buffer.find(b"\n")
- if newline_pos == -1:
- break
-
- line = buffer[:newline_pos]
- buffer = buffer[newline_pos + 1:]
-
- line_str = line.decode("utf-8", errors="replace").strip()
- if line_str:
- # Handle line and update extraction status
- async for result_event in self._handle_line_gen(line_str, session_id_extracted):
- if result_event.get("type") == "session_info":
- session_id_extracted = True
- yield result_event
-
- if self.process.stderr:
- stderr_output = await self.process.stderr.read()
- if stderr_output:
- logger.error(
- f"Claude CLI Stderr: {stderr_output.decode('utf-8', errors='replace')}"
- )
-
- return_code = await self.process.wait()
- logger.info(f"Claude CLI process exited with code {return_code}")
- yield {"type": "exit", "code": return_code}
- finally:
- self._is_busy = False
-
- async def _handle_line_gen(self, line_str: str, session_id_extracted: bool) -> AsyncGenerator[dict, None]:
- """Process a single line and yield events."""
- try:
- event = json.loads(line_str)
- # Extract session ID from various event types
- if not session_id_extracted:
- extracted_id = self._extract_session_id(event)
- if extracted_id:
- self.current_session_id = extracted_id
- logger.info(f"Extracted session ID: {extracted_id}")
- # Emit a special event with the session ID
- yield {"type": "session_info", "session_id": extracted_id}
-
- yield event
- except json.JSONDecodeError:
- logger.debug(f"Non-JSON output: {line_str}")
- yield {"type": "raw", "content": line_str}
-
- def _extract_session_id(self, event: Dict) -> Optional[str]:
- """
- Extract session ID from CLI event.
-
- The session ID appears in different places depending on event type:
- - In 'init' or 'system' events as 'session_id' or 'sessionId'
- - In 'result' events under result.session_id
- - Sometimes in message metadata
- """
- if not isinstance(event, dict):
- return None
-
- # Direct session_id field
- if "session_id" in event:
- return event["session_id"]
- if "sessionId" in event:
- return event["sessionId"]
-
- # Check in nested structures
- for key in ["init", "system", "result", "metadata"]:
- if key in event and isinstance(event[key], dict):
- nested = event[key]
- if "session_id" in nested:
- return nested["session_id"]
- if "sessionId" in nested:
- return nested["sessionId"]
-
- # Check in conversation info
- if "conversation" in event and isinstance(event["conversation"], dict):
- conv = event["conversation"]
- if "id" in conv:
- return conv["id"]
-
- return None
-
- async def stop(self):
- if self.process and self.process.returncode is None:
- try:
- logger.info(f"Stopping Claude CLI process {self.process.pid}")
- self.process.terminate()
- try:
- await asyncio.wait_for(self.process.wait(), timeout=5.0)
- except asyncio.TimeoutError:
- logger.warning(f"Process {self.process.pid} did not terminate, killing...")
- self.process.kill()
- await self.process.wait()
- return True
- except Exception as e:
- logger.error(f"Error stopping session process: {e}")
- return False
- return False
diff --git a/providers/cli_session_manager.py b/providers/cli_session_manager.py
deleted file mode 100644
index bf6786af..00000000
--- a/providers/cli_session_manager.py
+++ /dev/null
@@ -1,216 +0,0 @@
-"""
-CLI Session Manager for Multi-Instance Claude CLI Support
-
-Manages a pool of CLISession instances, each handling one conversation.
-This enables true parallel processing where multiple conversations run
-simultaneously in separate CLI processes.
-"""
-
-import asyncio
-import uuid
-import logging
-from typing import Dict, Optional, Tuple, List
-from .claude_cli import CLISession
-
-logger = logging.getLogger(__name__)
-
-
-class CLISessionManager:
- """
- Manages multiple CLISession instances for parallel conversation processing.
-
- Each new conversation gets its own CLISession with its own subprocess.
- Replies to existing conversations reuse the same CLISession instance.
- """
-
- def __init__(
- self,
- workspace_path: str,
- api_url: str,
- allowed_dirs: Optional[List[str]] = None,
- max_sessions: int = 10,
- ):
- """
- Initialize the session manager.
-
- Args:
- workspace_path: Working directory for CLI processes
- api_url: API URL for the proxy
- allowed_dirs: Directories the CLI is allowed to access
- max_sessions: Maximum concurrent sessions (prevents resource exhaustion)
- """
- self.workspace = workspace_path
- self.api_url = api_url
- self.allowed_dirs = allowed_dirs or []
- self.max_sessions = max_sessions
-
- # Active sessions: real_session_id -> CLISession
- self._sessions: Dict[str, CLISession] = {}
-
- # Pending sessions: temp_id -> CLISession (before we know real session ID)
- self._pending_sessions: Dict[str, CLISession] = {}
-
- # Mapping: temp_id -> real_session_id (for updating after CLI responds)
- self._temp_to_real: Dict[str, str] = {}
-
- # Lock for thread-safe session management
- self._lock = asyncio.Lock()
-
- logger.info(f"CLISessionManager initialized (max_sessions={max_sessions})")
-
- async def get_or_create_session(
- self, session_id: Optional[str] = None
- ) -> Tuple[CLISession, str, bool]:
- """
- Get an existing session or create a new one.
-
- Args:
- session_id: Optional existing session ID to resume.
- If None, creates a new session.
-
- Returns:
- Tuple of (CLISession instance, session_id, is_new_session)
- For new sessions, session_id is a temporary ID until CLI assigns real one.
- """
- async with self._lock:
- # Case 1: Resume existing session (active or pending)
- if session_id:
- # Resolve temp_id to real_id if needed
- lookup_id = self._temp_to_real.get(session_id, session_id)
-
- if lookup_id in self._sessions:
- logger.debug(f"Reusing existing session: {lookup_id}")
- return self._sessions[lookup_id], lookup_id, False
- if lookup_id in self._pending_sessions:
- logger.debug(f"Reusing pending session: {lookup_id}")
- return self._pending_sessions[lookup_id], lookup_id, False
-
- # Case 2: Check if we're at capacity
- total_sessions = len(self._sessions) + len(self._pending_sessions)
- if total_sessions >= self.max_sessions:
- # Find and clean up any idle sessions
- await self._cleanup_idle_sessions_unlocked()
-
- # Re-check after cleanup
- total_sessions = len(self._sessions) + len(self._pending_sessions)
- if total_sessions >= self.max_sessions:
- logger.warning(f"Max sessions ({self.max_sessions}) reached")
- raise RuntimeError(
- f"Maximum concurrent sessions ({self.max_sessions}) reached. "
- "Please wait for existing conversations to complete."
- )
-
- # Case 3: Create new session
- # If session_id was provided (but not found), use it as temp_id
- # Otherwise generate a new one
- temp_id = session_id if session_id else f"pending_{uuid.uuid4().hex[:8]}"
-
- new_session = CLISession(
- workspace_path=self.workspace,
- api_url=self.api_url,
- allowed_dirs=self.allowed_dirs,
- )
- self._pending_sessions[temp_id] = new_session
- logger.info(f"Created new session with temp_id: {temp_id}")
-
- return new_session, temp_id, True
-
- async def register_real_session_id(self, temp_id: str, real_session_id: str) -> bool:
- """
- Called when we learn the real session ID from CLI output.
- Moves session from pending to active.
-
- Args:
- temp_id: The temporary ID we assigned
- real_session_id: The real session ID from Claude CLI
-
- Returns:
- True if registration succeeded, False otherwise
- """
- async with self._lock:
- if temp_id not in self._pending_sessions:
- logger.warning(f"Temp session {temp_id} not found for registration")
- return False
-
- session = self._pending_sessions.pop(temp_id)
- self._sessions[real_session_id] = session
- self._temp_to_real[temp_id] = real_session_id
-
- logger.info(f"Registered session: {temp_id} -> {real_session_id}")
- return True
-
- async def get_real_session_id(self, temp_id: str) -> Optional[str]:
- """Get the real session ID for a temporary ID."""
- async with self._lock:
- return self._temp_to_real.get(temp_id)
-
- async def remove_session(self, session_id: str) -> bool:
- """
- Remove a session from the manager.
-
- Args:
- session_id: Session ID to remove (can be temp or real)
-
- Returns:
- True if session was removed, False if not found
- """
- async with self._lock:
- # Check pending sessions
- if session_id in self._pending_sessions:
- session = self._pending_sessions.pop(session_id)
- await session.stop()
- logger.info(f"Removed pending session: {session_id}")
- return True
-
- # Check active sessions
- if session_id in self._sessions:
- session = self._sessions.pop(session_id)
- await session.stop()
- # Clean up temp mapping
- for temp, real in list(self._temp_to_real.items()):
- if real == session_id:
- del self._temp_to_real[temp]
- logger.info(f"Removed active session: {session_id}")
- return True
-
- return False
-
- async def _cleanup_idle_sessions_unlocked(self):
- """
- Clean up sessions that are no longer busy.
- Must be called while holding self._lock.
- """
- idle_sessions = []
-
- for session_id, session in self._sessions.items():
- if not session.is_busy:
- idle_sessions.append(session_id)
-
- for session_id in idle_sessions[:3]: # Remove up to 3 idle sessions
- session = self._sessions.pop(session_id)
- await session.stop()
- logger.debug(f"Cleaned up idle session: {session_id}")
-
- async def stop_all(self):
- """Stop all active sessions. Called on shutdown."""
- async with self._lock:
- all_sessions = list(self._sessions.values()) + list(self._pending_sessions.values())
- for session in all_sessions:
- try:
- await session.stop()
- except Exception as e:
- logger.error(f"Error stopping session: {e}")
-
- self._sessions.clear()
- self._pending_sessions.clear()
- self._temp_to_real.clear()
- logger.info("All sessions stopped")
-
- def get_stats(self) -> Dict:
- """Get current session statistics."""
- return {
- "active_sessions": len(self._sessions),
- "pending_sessions": len(self._pending_sessions),
- "max_sessions": self.max_sessions,
- "busy_count": sum(1 for s in self._sessions.values() if s.is_busy),
- }
diff --git a/providers/message_queue.py b/providers/message_queue.py
deleted file mode 100644
index 8303f4d8..00000000
--- a/providers/message_queue.py
+++ /dev/null
@@ -1,200 +0,0 @@
-"""
-Message Queue Manager for Claude CLI Telegram Bot
-
-Handles queuing of messages when Claude is busy processing a request.
-Messages are processed one-by-one in order per session.
-"""
-
-import asyncio
-import logging
-from typing import Callable, Awaitable, Dict, Optional, NamedTuple, List, Any
-from dataclasses import dataclass
-
-logger = logging.getLogger(__name__)
-
-
-@dataclass
-class QueuedMessage:
- """A message waiting to be processed."""
- prompt: str
- chat_id: int
- msg_id: int
- reply_msg_id: int # The status message to update
- event: Any # Original Telegram event for context
-
-
-class SessionQueue:
- """Queue for a single session."""
-
- def __init__(self, session_id: str):
- self.session_id = session_id
- self.queue: asyncio.Queue[QueuedMessage] = asyncio.Queue()
- self.is_processing = False
- self.current_task: Optional[asyncio.Task] = None
- self.current_message: Optional[QueuedMessage] = None
-
-
-class MessageQueueManager:
- """
- Manages per-session message queues.
-
- When a session is busy, new messages are queued and processed
- one-by-one after the current request completes.
- """
-
- def __init__(self):
- self._queues: Dict[str, SessionQueue] = {}
- self._lock = asyncio.Lock()
-
- def _get_or_create_queue(self, session_id: str) -> SessionQueue:
- """Get existing queue or create new one for session."""
- if session_id not in self._queues:
- self._queues[session_id] = SessionQueue(session_id)
- return self._queues[session_id]
-
- def is_session_busy(self, session_id: str) -> bool:
- """Check if a session is currently processing a request."""
- if session_id not in self._queues:
- return False
- return self._queues[session_id].is_processing
-
- async def enqueue(
- self,
- session_id: str,
- message: QueuedMessage,
- processor: Callable[[str, QueuedMessage], Awaitable[None]],
- ) -> bool:
- """
- Add a message to the session's queue.
-
- If the session is not busy, processing starts immediately.
- If busy, the message is queued for later processing.
-
- Args:
- session_id: Claude session ID
- message: The queued message data
- processor: Async function to process the message
-
- Returns:
- True if message was queued (session busy), False if processed immediately
- """
- async with self._lock:
- sq = self._get_or_create_queue(session_id)
-
- if sq.is_processing:
- # Session is busy, queue the message
- await sq.queue.put(message)
- queue_size = sq.queue.qsize()
- logger.info(f"Queued message for session {session_id}, queue size: {queue_size}")
- return True
- else:
- # Session is free, start processing
- sq.is_processing = True
-
- # Process outside the lock
- sq = self._queues[session_id]
- sq.current_task = asyncio.create_task(self._process_message(session_id, message, processor))
- return False
-
- async def _process_message(
- self,
- session_id: str,
- message: QueuedMessage,
- processor: Callable[[str, QueuedMessage], Awaitable[None]],
- ) -> None:
- """Process a single message and then check the queue."""
- sq = self._queues.get(session_id)
- if sq:
- sq.current_message = message
- try:
- await processor(session_id, message)
- except asyncio.CancelledError:
- logger.info(f"Task for session {session_id} was cancelled")
- raise
- except Exception as e:
- logger.error(f"Error processing message for session {session_id}: {e}")
- finally:
- if sq:
- sq.current_message = None
- # Check if there are more messages in the queue
- await self._process_next(session_id, processor)
-
- async def _process_next(
- self,
- session_id: str,
- processor: Callable[[str, QueuedMessage], Awaitable[None]],
- ) -> None:
- """Process the next message in queue, if any."""
- async with self._lock:
- if session_id not in self._queues:
- return
-
- sq = self._queues[session_id]
-
- if sq.queue.empty():
- # No more messages, mark session as free
- sq.is_processing = False
- logger.debug(f"Session {session_id} queue empty, marking as free")
- return
-
- # Get next message
- try:
- next_msg = sq.queue.get_nowait()
- logger.info(f"Processing next queued message for session {session_id}")
- except asyncio.QueueEmpty:
- sq.is_processing = False
- return
-
- # Process next message (outside lock)
- sq.current_task = asyncio.create_task(self._process_message(session_id, next_msg, processor))
-
- def get_queue_size(self, session_id: str) -> int:
- """Get the number of messages waiting in a session's queue."""
- if session_id not in self._queues:
- return 0
- return self._queues[session_id].queue.qsize()
-
- def cancel_session(self, session_id: str) -> List[QueuedMessage]:
- """
- Cancel all queued messages for a session and the running task.
-
- Returns:
- List of messages that were cancelled (including the current one if any)
- """
- if session_id not in self._queues:
- return []
-
- sq = self._queues[session_id]
- cancelled_messages = []
-
- # 1. Cancel running task
- if sq.current_task and not sq.current_task.done():
- sq.current_task.cancel()
- if sq.current_message:
- cancelled_messages.append(sq.current_message)
-
- # 2. Clear queue
- while not sq.queue.empty():
- try:
- msg = sq.queue.get_nowait()
- cancelled_messages.append(msg)
- except asyncio.QueueEmpty:
- break
-
- sq.is_processing = False
- logger.info(f"Cancelled {len(cancelled_messages)} messages for session {session_id}")
- return cancelled_messages
-
- async def cancel_all(self) -> List[QueuedMessage]:
- """
- Cancel everything in all sessions.
-
- Returns:
- List of all cancelled messages across all sessions.
- """
- async with self._lock:
- all_cancelled = []
- session_ids = list(self._queues.keys())
- for sid in session_ids:
- all_cancelled.extend(self.cancel_session(sid))
- return all_cancelled
diff --git a/providers/session_store.py b/providers/session_store.py
deleted file mode 100644
index b89c8208..00000000
--- a/providers/session_store.py
+++ /dev/null
@@ -1,190 +0,0 @@
-"""
-Session Store for Claude CLI Telegram Bot
-
-Provides persistent storage for mapping Telegram messages to Claude CLI session IDs.
-This enables conversation continuation when replying to old messages.
-"""
-
-import json
-import os
-import logging
-from datetime import datetime
-from typing import Optional, Dict, Any
-from dataclasses import dataclass, asdict
-import threading
-
-logger = logging.getLogger(__name__)
-
-
-@dataclass
-class SessionRecord:
- """A single session record."""
- session_id: str
- chat_id: int
- initial_msg_id: int # The first message that started this session
- last_msg_id: int # Most recent message in this session
- created_at: str
- updated_at: str
-
-
-class SessionStore:
- """
- Persistent storage for Telegram message ↔ Claude session mappings.
-
- Uses a JSON file for storage with thread-safe operations.
- """
-
- def __init__(self, storage_path: str = "sessions.json"):
- self.storage_path = storage_path
- self._lock = threading.Lock()
- self._sessions: Dict[str, SessionRecord] = {} # session_id -> record
- self._msg_to_session: Dict[str, str] = {} # "chat_id:msg_id" -> session_id
- self._load()
-
- def _make_key(self, chat_id: int, msg_id: int) -> str:
- """Create a unique key from chat_id and msg_id."""
- return f"{chat_id}:{msg_id}"
-
- def _load(self) -> None:
- """Load sessions from disk."""
- if not os.path.exists(self.storage_path):
- return
-
- try:
- with open(self.storage_path, 'r', encoding='utf-8') as f:
- data = json.load(f)
-
- for sid, record_data in data.get("sessions", {}).items():
- record = SessionRecord(**record_data)
- self._sessions[sid] = record
- # Index by initial and last message
- self._msg_to_session[self._make_key(record.chat_id, record.initial_msg_id)] = sid
- self._msg_to_session[self._make_key(record.chat_id, record.last_msg_id)] = sid
-
- logger.info(f"Loaded {len(self._sessions)} sessions from {self.storage_path}")
- except Exception as e:
- logger.error(f"Failed to load sessions: {e}")
-
- def _save(self) -> None:
- """Persist sessions to disk."""
- try:
- data = {
- "sessions": {sid: asdict(record) for sid, record in self._sessions.items()}
- }
- with open(self.storage_path, 'w', encoding='utf-8') as f:
- json.dump(data, f, indent=2)
- except Exception as e:
- logger.error(f"Failed to save sessions: {e}")
-
- def save_session(
- self,
- session_id: str,
- chat_id: int,
- initial_msg_id: int,
- ) -> None:
- """
- Save a new session mapping.
-
- Args:
- session_id: Claude CLI session ID
- chat_id: Telegram chat ID
- initial_msg_id: The message ID that started this session
- """
- with self._lock:
- now = datetime.utcnow().isoformat()
- record = SessionRecord(
- session_id=session_id,
- chat_id=chat_id,
- initial_msg_id=initial_msg_id,
- last_msg_id=initial_msg_id,
- created_at=now,
- updated_at=now,
- )
- self._sessions[session_id] = record
- self._msg_to_session[self._make_key(chat_id, initial_msg_id)] = session_id
- self._save()
- logger.info(f"Saved session {session_id} for chat {chat_id}, msg {initial_msg_id}")
-
- def get_session_by_msg(self, chat_id: int, msg_id: int) -> Optional[str]:
- """
- Look up a session ID by a message that's part of that session.
-
- Args:
- chat_id: Telegram chat ID
- msg_id: Message ID to look up
-
- Returns:
- Session ID if found, None otherwise
- """
- with self._lock:
- key = self._make_key(chat_id, msg_id)
- return self._msg_to_session.get(key)
-
- def update_last_message(self, session_id: str, msg_id: int) -> None:
- """
- Update the last message ID for a session.
-
- This is called when we send a new response in an existing session,
- so replies to that response will also continue the session.
-
- Args:
- session_id: Claude session ID
- msg_id: New last message ID
- """
- with self._lock:
- if session_id not in self._sessions:
- logger.warning(f"Session {session_id} not found for update")
- return
-
- record = self._sessions[session_id]
- old_key = self._make_key(record.chat_id, record.last_msg_id)
-
- # Update record
- record.last_msg_id = msg_id
- record.updated_at = datetime.utcnow().isoformat()
-
- # Update index - add new key, keep old one for chain lookups
- new_key = self._make_key(record.chat_id, msg_id)
- self._msg_to_session[new_key] = session_id
-
- self._save()
- logger.debug(f"Updated session {session_id} last_msg to {msg_id}")
-
- def get_session_record(self, session_id: str) -> Optional[SessionRecord]:
- """Get full session record."""
- with self._lock:
- return self._sessions.get(session_id)
-
- def cleanup_old_sessions(self, max_age_days: int = 30) -> int:
- """
- Remove sessions older than max_age_days.
-
- Returns:
- Number of sessions removed
- """
- with self._lock:
- cutoff = datetime.utcnow()
- removed = 0
-
- to_remove = []
- for sid, record in self._sessions.items():
- try:
- created = datetime.fromisoformat(record.created_at)
- age_days = (cutoff - created).days
- if age_days > max_age_days:
- to_remove.append(sid)
- except Exception:
- pass
-
- for sid in to_remove:
- record = self._sessions.pop(sid)
- # Remove index entries
- self._msg_to_session.pop(self._make_key(record.chat_id, record.initial_msg_id), None)
- self._msg_to_session.pop(self._make_key(record.chat_id, record.last_msg_id), None)
- removed += 1
-
- if removed:
- self._save()
- logger.info(f"Cleaned up {removed} old sessions")
-
- return removed
diff --git a/tests/test_limiter.py b/tests/test_limiter.py
new file mode 100644
index 00000000..c2dd6fd8
--- /dev/null
+++ b/tests/test_limiter.py
@@ -0,0 +1,163 @@
+import pytest
+import pytest_asyncio
+import asyncio
+import time
+import os
+import logging
+from unittest.mock import MagicMock
+
+# Set environment variables relative to test execution
+os.environ["MESSAGING_RATE_LIMIT"] = "1"
+os.environ["MESSAGING_RATE_WINDOW"] = "0.5"
+
+from messaging.limiter import GlobalRateLimiter
+
+# Configure logging for tests
+logging.basicConfig(level=logging.INFO)
+logger = logging.getLogger(__name__)
+
+
+class TestGlobalRateLimiter:
+ """Tests for GlobalRateLimiter."""
+
+ @pytest_asyncio.fixture(autouse=True)
+ async def reset_limiter(self):
+ """Reset singleton and environment before each test."""
+ # Reset the singleton manually if needed (although __init__ protection exists)
+ if GlobalRateLimiter._instance:
+ # Stop worker if possible or just reset instance
+ pass
+ GlobalRateLimiter._instance = None
+ os.environ["MESSAGING_RATE_LIMIT"] = "1"
+ os.environ["MESSAGING_RATE_WINDOW"] = "0.5"
+
+ yield
+
+ GlobalRateLimiter._instance = None
+
+ @pytest.mark.asyncio
+ async def test_singleton_pattern(self):
+ """Test that get_instance returns the same object."""
+ limiter1 = await GlobalRateLimiter.get_instance()
+ limiter2 = await GlobalRateLimiter.get_instance()
+ assert limiter1 is limiter2
+
+ @pytest.mark.asyncio
+ async def test_compaction(self):
+ """
+ Verify multiple rapid requests with same dedup_key are compacted.
+ Logic ported from verify_limiter.py
+ """
+ # Set slow rate for testing compaction
+ os.environ["MESSAGING_RATE_LIMIT"] = "1"
+ os.environ["MESSAGING_RATE_WINDOW"] = "1.0"
+
+ # Must reset instance to pick up new env vars
+ GlobalRateLimiter._instance = None
+ limiter = await GlobalRateLimiter.get_instance()
+
+ call_counts = {}
+
+ async def mock_edit(msg_id, content):
+ call_counts[msg_id] = call_counts.get(msg_id, 0) + 1
+ return f"done_{content}"
+
+ # Spam 5 edits
+ for i in range(5):
+ limiter.fire_and_forget(
+ lambda i=i: mock_edit("msg1", f"update_{i}"), dedup_key="edit:msg1"
+ )
+
+ # Wait for processing
+ # 1st might go through immediately, subsequent ones queue and compact
+ await asyncio.sleep(2.5)
+
+ # Expected: ~2 calls (first and last)
+ assert call_counts["msg1"] <= 2, (
+ f"Expected compaction to reduce calls, but got {call_counts.get('msg1', 0)}"
+ )
+ assert call_counts["msg1"] >= 1, "Expected at least one call"
+
+ @pytest.mark.asyncio
+ async def test_compaction_and_futures_resolution(self):
+ """
+ Verify that even when compacted, all futures resolve to the result of the LAST execution.
+ Logic ported from verify_limiter_v2.py
+ """
+ os.environ["MESSAGING_RATE_LIMIT"] = "1"
+ os.environ["MESSAGING_RATE_WINDOW"] = "0.5"
+ GlobalRateLimiter._instance = None
+ limiter = await GlobalRateLimiter.get_instance()
+
+ call_counts = {}
+ msg_id = "test_msg_hang"
+
+ async def mock_edit(mid, content):
+ call_counts[mid] = call_counts.get(mid, 0) + 1
+ await asyncio.sleep(0.05)
+ return f"result_{content}"
+
+ async def task(i):
+ return await limiter.enqueue(
+ lambda i=i: mock_edit(msg_id, f"v{i}"), dedup_key=f"edit:{msg_id}"
+ )
+
+ start_time = time.time()
+
+ # Enqueue 3 tasks concurrently
+ results = await asyncio.gather(task(1), task(2), task(3))
+
+ duration = time.time() - start_time
+
+ # All results should be the LAST one executed
+ for res in results:
+ assert res == "result_v3", f"Expected result_v3, got {res}"
+
+ # Should be reasonably fast
+ assert duration < 2.0, "Execution took too long"
+
+ # Calls should be compacted
+ assert call_counts[msg_id] <= 2, f"Too many actual calls: {call_counts[msg_id]}"
+
+ @pytest.mark.asyncio
+ async def test_flood_wait_handling(self):
+ """Test that FloodWait exceptions pause the worker."""
+ GlobalRateLimiter._instance = None
+ limiter = await GlobalRateLimiter.get_instance()
+
+ # Mock exception with .seconds attribute
+ class FloodWait(Exception):
+ def __init__(self, seconds):
+ self.seconds = seconds
+ super().__init__(f"Flood wait {seconds}s")
+
+ call_count = 0
+
+ async def mock_fail():
+ nonlocal call_count
+ call_count += 1
+ raise FloodWait(1) # 1 second wait
+
+ async def mock_success():
+ nonlocal call_count
+ call_count += 1
+ return "success"
+
+ # First call fails and triggers pause
+ try:
+ await limiter.enqueue(mock_fail, dedup_key="key1")
+ except Exception:
+ pass # Expected
+
+ assert limiter._paused_until > 0
+
+ # Enqueue success, it should wait
+ start = time.time()
+ await limiter.enqueue(mock_success, dedup_key="key2")
+ duration = time.time() - start
+
+ # Should have waited at least ~1s
+ assert duration >= 0.9, (
+ f"Should have waited for FloodWait, but took {duration:.2f}s"
+ )
+ assert call_count == 2
diff --git a/tests/test_provider_rate_limit.py b/tests/test_provider_rate_limit.py
new file mode 100644
index 00000000..d907a9bc
--- /dev/null
+++ b/tests/test_provider_rate_limit.py
@@ -0,0 +1,95 @@
+import pytest
+import pytest_asyncio
+import asyncio
+import time
+import os
+import logging
+from unittest.mock import MagicMock
+
+from providers.rate_limit import GlobalRateLimiter
+
+# Configure logging for tests
+logging.basicConfig(level=logging.INFO)
+logger = logging.getLogger(__name__)
+
+
+class TestProviderRateLimiter:
+ """Tests for providers.rate_limit.GlobalRateLimiter."""
+
+ @pytest_asyncio.fixture(autouse=True)
+ async def reset_limiter(self):
+ """Reset singleton and environment before each test."""
+ GlobalRateLimiter.reset_instance()
+ # Defaults
+ os.environ["NVIDIA_NIM_RATE_LIMIT"] = "40"
+ os.environ["NVIDIA_NIM_RATE_WINDOW"] = "60.0"
+ yield
+ GlobalRateLimiter.reset_instance()
+
+ @pytest.mark.asyncio
+ async def test_proactive_throttling(self):
+ """
+ Test proactive throttling using aiolimiter.
+ Logic ported from verify_provider_limiter.py
+ """
+ # Set limit: 1 request per 0.25 second
+ os.environ["NVIDIA_NIM_RATE_LIMIT"] = "1"
+ os.environ["NVIDIA_NIM_RATE_WINDOW"] = "0.25"
+
+ # Re-init with new limits
+ GlobalRateLimiter.reset_instance()
+ limiter = GlobalRateLimiter.get_instance()
+
+ start_time = time.time()
+
+ async def call_limiter():
+ await limiter.wait_if_blocked()
+ return time.time()
+
+ # 5 requests.
+ # R0 -> 0s
+ # R1 -> 0.25s
+ # R2 -> 0.50s
+ # R3 -> 0.75s
+ # R4 -> 1.00s
+ results = []
+ for _ in range(5):
+ results.append(await call_limiter())
+
+ total_time = time.time() - start_time
+
+ assert len(results) == 5
+ # Should take at least ~1.0s
+ assert total_time >= 0.9, f"Throttling failed, took too fast: {total_time:.2f}s"
+
+ @pytest.mark.asyncio
+ async def test_reactive_blocking(self):
+ """
+ Test reactive blocking when set_blocked is called.
+ Logic ported from verify_provider_limiter.py
+ """
+ GlobalRateLimiter.reset_instance()
+ limiter = GlobalRateLimiter.get_instance()
+
+ start_time = time.time()
+
+ # Manually block for 1.5s
+ block_time = 1.5
+ limiter.set_blocked(block_time)
+
+ assert limiter.is_blocked()
+
+ async def call_limiter():
+ return await limiter.wait_if_blocked()
+
+ # Run 2 calls concurrently
+ # They should both wait for the block time
+ results = await asyncio.gather(call_limiter(), call_limiter())
+
+ total_time = time.time() - start_time
+
+ # Both should report having waited reactively
+ assert all(results) is True
+ assert total_time >= block_time - 0.1, (
+ f"Reactive block failed, took {total_time:.2f}s"
+ )
diff --git a/tests/verify_limiter.py b/tests/verify_limiter.py
deleted file mode 100644
index 1b78ed9f..00000000
--- a/tests/verify_limiter.py
+++ /dev/null
@@ -1,57 +0,0 @@
-import asyncio
-import time
-import os
-from messaging.limiter import GlobalRateLimiter
-
-# Mocking logging for test
-import logging
-
-logging.basicConfig(level=logging.INFO)
-logger = logging.getLogger(__name__)
-
-
-async def test_compaction():
- # Set small rate for testing
- os.environ["MESSAGING_RATE_LIMIT"] = "1"
- os.environ["MESSAGING_RATE_WINDOW"] = "1.0"
-
- limiter = await GlobalRateLimiter.get_instance()
-
- call_counts = {}
-
- async def mock_edit(msg_id, content):
- call_counts[msg_id] = call_counts.get(msg_id, 0) + 1
- logger.info(f"Executing edit for {msg_id}: {content}")
- return f"done_{content}"
-
- print("\n--- Starting Compaction Test ---")
-
- # Spam 5 edits for the same message
- start_time = time.time()
- futures = []
- for i in range(5):
- # We use fire_and_forget to fill the queue fast
- limiter.fire_and_forget(
- lambda i=i: mock_edit("msg1", f"update_{i}"), dedup_key="edit:msg1"
- )
- print(f"Queued update_{i}")
-
- # Wait for processing (should take ~2 seconds total for 1-2 actual calls)
- # The first one might go through immediately if limiter has capacity
- # The next 4 should be compacted into 1 if they arrive before the first one finishes?
- # Actually, the FIRST one is already "popped" by the worker while the loop is still queueing.
- # So we expect roughly 2 calls: the 1st one, and the LAST one (all intermediate ones compacted).
-
- await asyncio.sleep(2.5)
-
- print(f"\nTotal calls to Telegram: {sum(call_counts.values())}")
- print(f"Call breakdown: {call_counts}")
-
- assert call_counts["msg1"] <= 2, (
- f"Should have at most 2 calls (first and last), but got {call_counts['msg1']}"
- )
- print("✅ Compaction test passed!")
-
-
-if __name__ == "__main__":
- asyncio.run(test_compaction())
diff --git a/tests/verify_limiter_v2.py b/tests/verify_limiter_v2.py
deleted file mode 100644
index 736e6063..00000000
--- a/tests/verify_limiter_v2.py
+++ /dev/null
@@ -1,77 +0,0 @@
-import asyncio
-import time
-import os
-from messaging.limiter import GlobalRateLimiter
-
-import logging
-
-logging.basicConfig(level=logging.INFO)
-logger = logging.getLogger(__name__)
-
-
-async def test_compaction_and_hang():
- # Set small rate for testing
- os.environ["MESSAGING_RATE_LIMIT"] = "1"
- os.environ["MESSAGING_RATE_WINDOW"] = "0.5" # Fast for testing
-
- limiter = await GlobalRateLimiter.get_instance()
-
- call_counts = {}
-
- async def mock_edit(msg_id, content):
- call_counts[msg_id] = call_counts.get(msg_id, 0) + 1
- logger.info(f"Executing actual Telegram edit for {msg_id}: {content}")
- await asyncio.sleep(0.1) # Simulate network lag
- return f"result_{content}"
-
- print("\n--- Starting Hang/Multi-Future Test ---")
-
- msg_id = "test_msg_123"
-
- # We will enqueue 3 edits and await all of them.
- # Previously, the 2nd and 3rd would HANG.
-
- async def task(i):
- logger.info(f"Task {i} started, enqueuing edit...")
- res = await limiter.enqueue(
- lambda i=i: mock_edit(msg_id, f"v{i}"), dedup_key=f"edit:{msg_id}"
- )
- logger.info(f"Task {i} completed with: {res}")
- return res
-
- start_time = time.time()
-
- # Run tasks concurrently
- results = await asyncio.gather(task(1), task(2), task(3))
-
- end_time = time.time()
- duration = end_time - start_time
-
- print(f"\nAll tasks finished in {duration:.2f}s")
- print(f"Results: {results}")
- print(f"Call counts: {call_counts}")
-
- # Check that they all got the LAST result
- for res in results:
- assert res == "result_v3", f"Expected result_v3, got {res}"
-
- # Check that they didn't hang (should be < 1s given the compaction)
- assert duration < 2.0, "Tasks took too long, might have hung or not compacted"
-
- # Check call counts:
- # T1 might go through immediately or be compacted if T2/T3 arrive fast enough.
- # Given the loop speed, we expect 1-2 calls.
- assert call_counts[msg_id] <= 2, f"Too many calls: {call_counts[msg_id]}"
-
- print("\nPASSED: All futures resolved, no hang detected.")
-
-
-if __name__ == "__main__":
- # Fix encoding for windows terminal to avoid Unicode print crash
- import sys
- import io
-
- if sys.platform == "win32":
- sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding="utf-8")
-
- asyncio.run(test_compaction_and_hang())
diff --git a/tests/verify_provider_limiter.py b/tests/verify_provider_limiter.py
deleted file mode 100644
index 50c2bc82..00000000
--- a/tests/verify_provider_limiter.py
+++ /dev/null
@@ -1,87 +0,0 @@
-import asyncio
-import os
-import time
-import logging
-import sys
-
-# Add current directory to path
-sys.path.append(os.getcwd())
-
-from providers.rate_limit import GlobalRateLimiter
-
-logging.basicConfig(level=logging.INFO)
-logger = logging.getLogger(__name__)
-
-
-async def test_proactive_throttling():
- print("\n--- Testing Proactive Throttling (3 req / 1 sec) ---")
- os.environ["NVIDIA_NIM_RATE_LIMIT"] = "3"
- os.environ["NVIDIA_NIM_RATE_WINDOW"] = "1.0"
-
- GlobalRateLimiter.reset_instance()
- limiter = GlobalRateLimiter.get_instance()
-
- start_time = time.time()
-
- async def call_limiter(i):
- await limiter.wait_if_blocked()
- print(f"[{time.time() - start_time:.2f}s] Request {i} passed")
-
- print("Sending 5 requests sequentially...")
- # 3 should pass immediately, 4th and 5th should wait
- for i in range(5):
- await call_limiter(i)
-
- total_time = time.time() - start_time
- print(f"All requests completed in {total_time:.2f}s")
-
- # 5 requests with limit 3 per 1s:
- # R0, R1, R2 at ~0s
- # R3 waits until R0 is 1s old -> ~1s
- # R4 waits until R1 is 1s old -> ~1s
- if total_time >= 0.9: # Allow some jitter
- print("[SUCCESS] Proactive throttling working!")
- else:
- print(f"[FAILURE] Proactive throttling failed! Took only {total_time:.2f}s")
-
-
-async def test_reactive_blocking():
- print("\n--- Testing Reactive Blocking ---")
- GlobalRateLimiter.reset_instance()
- limiter = GlobalRateLimiter.get_instance()
-
- start_time = time.time()
-
- print("Setting manual block for 2s...")
- limiter.set_blocked(2)
-
- async def call_limiter(i):
- waited = await limiter.wait_if_blocked()
- print(
- f"[{time.time() - start_time:.2f}s] Request {i} passed (waited reactively: {waited})"
- )
- return waited
-
- results = await asyncio.gather(*[call_limiter(i) for i in range(2)])
-
- total_time = time.time() - start_time
- print(f"Requests completed in {total_time:.2f}s")
-
- if total_time >= 1.9 and any(results):
- print("[SUCCESS] Reactive blocking working!")
- else:
- print(f"[FAILURE] Reactive blocking failed! Took only {total_time:.2f}s")
-
-
-async def main():
- try:
- await test_proactive_throttling()
- await test_reactive_blocking()
- except Exception as e:
- print(f"Error during verification: {e}")
- finally:
- print("\nVerification complete.")
-
-
-if __name__ == "__main__":
- asyncio.run(main())