# 🚀 Agent Performance - Shell Optimization

## 🎯 Problem

When agents run commands like:
```bash
zsh -lc 'rg "pattern" file.ts'
```

They get **NVM warnings** that slow down execution:
```
Your user's .npmrc file (${HOME}/.npmrc)
has a `globalconfig` and/or a `prefix` setting, which are incompatible with nvm.
Run `nvm use --delete-prefix v20.19.5 --silent` to unset it.
```

**Impact:**
- Extra ~100-200ms per command
- Cluttered output for agents
- Shell initialization overhead (.zshrc, .npmrc, etc.)

## ✅ Solutions

### Solution 1: Fix .npmrc (Recommended)

Remove conflicting settings from `~/.npmrc`:

```bash
# Backup current config
cp ~/.npmrc ~/.npmrc.backup

# Remove problematic lines
sed -i '' '/^globalconfig=/d' ~/.npmrc
sed -i '' '/^prefix=/d' ~/.npmrc

# Or manually edit and remove these lines:
# globalconfig=...
# prefix=...
```

**Result:** No more warnings, `zsh -lc` works clean

### Solution 2: Use rt-fast.sh (Bypass shell init)

Instead of agents calling:
```bash
zsh -lc 'rg "pattern" file.ts'
```

Use:
```bash
./scripts/rt-fast.sh search "pattern" file.ts
```

**Benefits:**
- No .zshrc loading
- No .npmrc warnings
- Direct binary calls
- Faster startup

### Solution 3: Conditional NVM loading in .zshrc

Add this to your `~/.zshrc`:

```bash
# Only load NVM in interactive shells
if [[ -o interactive ]]; then
    export NVM_DIR="$HOME/.nvm"
    [ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"
fi
```

**Result:** NVM only loads in real terminals, not in `zsh -lc`

### Solution 4: Silent NVM errors

Add to `~/.zshrc` before NVM loading:

```bash
# Silence NVM warnings
export NVM_SILENT=1

# Then load NVM
export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" 2>/dev/null
```

## 📊 Performance Comparison

### With Shell Init (current)
```bash
$ time zsh -lc 'rg "pattern" file.ts'
real    0m0.428s   # Slow due to .zshrc/.npmrc
```

### With rt-fast.sh (optimized)
```bash
$ time ./scripts/rt-fast.sh search "pattern" file.ts
real    0m0.314s   # ~25% faster, no warnings
```

### Direct ripgrep (theoretical max)
```bash
$ time /opt/homebrew/bin/rg "pattern" file.ts
real    0m0.015s   # Fastest, but requires full path
```

## 🎛️ rt-fast.sh Commands

```bash
# Search in files
./scripts/rt-fast.sh search "pattern" path/to/file.ts

# Find files
./scripts/rt-fast.sh find-file "*.vue"

# Count matches
./scripts/rt-fast.sh count "TODO" apps/

# List imports
./scripts/rt-fast.sh list-imports path/to/file.ts

# Find usage
./scripts/rt-fast.sh find-usage "myFunction" apps/

# Direct ripgrep
./scripts/rt-fast.sh rg -n "pattern" file.ts

# Direct fd
./scripts/rt-fast.sh fd -e ts
```

## 🔧 Recommended Setup

**Step 1: Fix .npmrc**
```bash
# Remove conflicting settings
sed -i '' '/^globalconfig=/d' ~/.npmrc
sed -i '' '/^prefix=/d' ~/.npmrc
```

**Step 2: Optimize .zshrc**
```bash
# Add conditional NVM loading
cat >> ~/.zshrc << 'EOF'

# Only load NVM in interactive shells (not for agents)
if [[ -o interactive ]]; then
    export NVM_DIR="$HOME/.nvm"
    [ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" 2>/dev/null
fi
EOF
```

**Step 3: Create alias for agents**
```bash
# Add to scripts/rt-aliases.sh
alias rtf='~/Documents/MyRoadTrip/scripts/rt-fast.sh'
```

## 🎯 For AI Agents

**Instead of:**
```bash
zsh -lc 'rg "isMobileViewport" -n apps/web/src/modules/planner/pages/DashboardPage.vue'
```

**Use:**
```bash
~/Documents/MyRoadTrip/scripts/rt-fast.sh search "isMobileViewport" apps/web/src/modules/planner/pages/DashboardPage.vue
```

**Or even better (if in PATH):**
```bash
rt-fast search "isMobileViewport" apps/web/src/modules/planner/pages/DashboardPage.vue
```

## 📈 Expected Improvements

| Metric | Before | After | Improvement |
|--------|--------|-------|-------------|
| Command time | 428ms | 314ms | **26% faster** |
| Output clutter | NVM warnings | Clean | **100% cleaner** |
| Shell overhead | .zshrc + .npmrc | None | **Eliminated** |

## 🚀 Next Steps

1. **Fix .npmrc** (1 minute)
   ```bash
   sed -i '' '/^globalconfig=/d' ~/.npmrc
   sed -i '' '/^prefix=/d' ~/.npmrc
   ```

2. **Test it works**
   ```bash
   zsh -lc 'echo "test"'  # Should have no warnings
   ```

3. **Use rt-fast.sh for agent commands**
   ```bash
   ./scripts/rt-fast.sh search "pattern" file.ts
   ```

4. **Add to PATH** (optional)
   ```bash
   echo 'export PATH="$HOME/Documents/MyRoadTrip/scripts:$PATH"' >> ~/.zshrc
   source ~/.zshrc
   ```

---

**Result: Agent commands are now 25%+ faster with zero warnings!** ✨
