Design Rationale
Instead of running a single backend and restarting on venv changes, typemux-cc maintains a pool of concurrent backend processes — one per venv. This eliminates restart overhead when switching between projects in a monorepo.Before: Single Backend
- Switch venv → kill process → spawn new → re-index
- 5-10s downtime per switch
- Lost state on every transition
After: Multi-Backend Pool
- Keep multiple backends alive
- Instant switching (already indexed)
- Preserve state per venv
Pool Configuration
Basic Settings
| Parameter | CLI Flag | Environment Variable | Default | Description |
|---|---|---|---|---|
| Max backends | --max-backends | TYPEMUX_CC_MAX_BACKENDS | 8 | Upper limit on concurrent backend processes |
| Backend TTL | --backend-ttl | TYPEMUX_CC_BACKEND_TTL | 1800 (30 min) | Idle timeout in seconds (0 = disabled) |
Recommended Settings by Use Case
Monorepo with 2-3 projects
Monorepo with 2-3 projects
- Keeps 1 backend per project + 1 spare
- 30-minute TTL cleans up unused backends
Large monorepo (5+ projects)
Large monorepo (5+ projects)
- Higher pool size for frequent project switches
- 1-hour TTL for long coding sessions
Single project (no monorepo)
Single project (no monorepo)
- Minimal pool (1 backend + 1 spare for nested venvs)
- Disable TTL (backend never evicted)
Memory-constrained environments
Memory-constrained environments
- Limit concurrent backends to 2
- Aggressive 10-minute TTL
Backend Instance Structure
Each backend in the pool is represented by aBackendInstance (src/backend_pool.rs:44-55):
LRU Eviction Strategy
When the pool reachesmax_backends and a new backend is needed, typemux-cc evicts the least recently used (LRU) backend.
LRU Selection Algorithm
Code reference:src/backend_pool.rs:156-174
Eviction Sequence
What Happens During Eviction
- Cancel pending requests: All client requests waiting for responses from this backend receive a cancellation error (
-32800) - Clean up backend requests: Remove any pending backend→client requests from tracking
- Clear diagnostics: Send empty diagnostic messages to client to clear stale errors from evicted backend
- Shutdown process: Send LSP
shutdown+exitto backend, kill process if unresponsive - Abort reader task: Stop the background task reading from backend’s stdout
TTL-Based Eviction
Backends idle for longer thanbackend_ttl are automatically evicted to free resources.
TTL Sweep Mechanism
- Interval: 60 seconds (hardcoded in
src/proxy/mod.rsevent loop) - Check:
Instant::now() - last_used >= backend_ttl - Safety: Skip backends with pending requests (both client→backend and backend→client)
src/proxy/pool_management.rs:95-166
TTL Behavior Examples
- Default TTL (30 min)
- Short TTL (10 min)
- Disabled TTL
- 10:00 AM: User opens file in project-a → backend spawned
- 10:30 AM: User switches to project-b → project-a backend becomes idle
- 11:00 AM: TTL expires, project-a backend evicted (pool size: 1)
Session Tracking
Session ID Generation
Each backend gets a unique, monotonically increasing session ID when spawned: Code reference:src/backend_pool.rs:176-180
Session Validation
Every message received from a backend includes its session ID. Before processing, the proxy checks: Code reference:src/proxy/backend_dispatch.rs:23-49
Why Session IDs Matter
Without Session IDs
Problem: Backend crashes, new backend spawned with same venv path. Old responses arrive → forwarded to client → wrong data.Example:
- Backend session 1 serves project-a/.venv
- Client sends request ID 42
- Backend session 1 crashes
- New backend session 2 spawned for project-a/.venv
- Old response from session 1 arrives (wrong index state)
- ❌ Client receives stale response
With Session IDs
Solution: Responses from old sessions are discarded.Example:
- Backend session 1 serves project-a/.venv
- Client sends request ID 42 (recorded as session 1)
- Backend session 1 crashes
- New backend session 2 spawned for project-a/.venv
- Old response from session 1 arrives
- ✅ Proxy discards (session 1 != session 2)
- Client receives cancellation error for request 42
Pool State Inspection
Current Pool Status
To see which backends are in the pool:Pool Activity Monitoring
Memory Considerations
Each backend process (pyright/ty/pyrefly) typically uses:| Backend | Typical Memory | Peak Memory | Notes |
|---|---|---|---|
| pyright | 100-300 MB | 500 MB | Higher for large codebases |
| ty | 200-400 MB | 800 MB | Rust-based, aggressive caching |
| pyrefly | 150-350 MB | 600 MB | Similar to pyright |