# VS Code Session Limits & Crash Prevention Guide

## 🚨 Current Issues Identified

### 1. **High Process Count**: 38 VS Code processes running
### 2. **Memory Consumption**: 
   - Pylance server: 573MB
   - Utility processes: 686MB  
   - Total: ~1.2GB VS Code memory usage

### 3. **Recent Crashes**:
   - Renderer process crashes (code: 5) 
   - File watcher crashes (code: 15)
   - Extension host failures

## ⚙️ Solutions Implemented

### 1. **Optimized Settings** (`.vscode/settings.json`)
- Reduced editor suggestions and hover
- Limited Python analysis depth 
- Disabled auto-updates and telemetry
- Configured memory-efficient file watching

### 2. **Session Monitoring** (`scripts/vscode_session_monitor.sh`)
```bash
# Check current session status
./scripts/vscode_session_monitor.sh check

# Start continuous monitoring  
./scripts/vscode_session_monitor.sh monitor

# Clean up logs and cache
./scripts/vscode_session_monitor.sh cleanup
```

### 3. **Emergency Management** (`scripts/vscode_emergency_cleanup.sh`)
```bash
# Show memory status
./scripts/vscode_emergency_cleanup.sh status

# Light cleanup (recommended during agent iterations)
./scripts/vscode_emergency_cleanup.sh gentle

# Heavy cleanup (when unresponsive)
./scripts/vscode_emergency_cleanup.sh aggressive  

# Full restart
./scripts/vscode_emergency_cleanup.sh restart
```

### 4. **Environment Configuration** (`.vscode/session_config.env`)
Load before starting VS Code:
```bash
source .vscode/session_config.env
code .
```

## 📊 Agent Session Limits

### GitHub Copilot Limits:
- **Max Tokens per Request**: 4,000
- **History Length**: 10 conversations
- **Request Timeout**: 30 seconds

### System Resource Limits:
- **Memory Warning**: When VS Code uses >1.5GB
- **Process Limit**: Maximum 20 VS Code processes
- **Max Session Duration**: 2 hours
- **Auto Cleanup**: Every 30 minutes

### Agent Iteration Limits:
- **Max Iterations**: 50 per session
- **Max Edit Size**: 10,000 characters
- **Timeout per Operation**: 3 minutes

## 🔧 Prevention Strategies

### 1. **Before Long Agent Sessions**:
```bash
# Clean up first
./scripts/vscode_emergency_cleanup.sh gentle

# Load optimized environment
source .vscode/session_config.env

# Start monitoring
./scripts/vscode_session_monitor.sh monitor &
```

### 2. **During Agent Iterations**:
- Monitor memory with `./scripts/vscode_emergency_cleanup.sh status`
- Use gentle cleanup if memory >1GB
- Restart VS Code every 2 hours or 50+ operations

### 3. **Warning Signs**:
- VS Code becomes slow/unresponsive
- High CPU usage (>80%)
- Memory usage >1.5GB
- More than 30 processes

### 4. **Emergency Actions**:
```bash
# If VS Code freezes during agent work:
./scripts/vscode_emergency_cleanup.sh aggressive

# If completely unresponsive:
./scripts/vscode_emergency_cleanup.sh full
```

## 📈 Monitoring Commands

### Resource Monitoring:
```bash
# Check memory usage
free -h

# VS Code processes
ps aux | grep code | wc -l

# Memory per process  
ps -o pid,ppid,pcpu,pmem,rss,args -C code
```

### Log Analysis:
```bash
# Recent crashes
grep "crashed" ~/.config/Code/logs/*/main.log | tail -5

# Copilot requests
find ~/.config/Code/logs -name "*copilot*" -exec tail -10 {} \;

# Extension host status
ps aux | grep extensionHost
```

## 🎯 Recommended Workflow

1. **Before starting**: Load session config and monitor
2. **During agent work**: Check status every 15-20 operations  
3. **After 1 hour**: Perform gentle cleanup
4. **After 2 hours**: Restart VS Code completely
5. **If crashes occur**: Use emergency cleanup and restart

This configuration should prevent most crashes and provide early warning when limits are approached.