# Auto Start Wrapper Script

## Overview

The `auto-start-wrapper.sh` script provides **reliable Ctrl+C handling** for `vcm auto:start`.

## Problem

Due to a Node.js limitation, SIGINT (Ctrl+C) signals don't reliably reach the event loop when Node.js is blocked waiting for child processes (like Aider). **This means Ctrl+C does NOT work when running `vcm auto:start` directly.**

## Recommended Usage

**If you want Ctrl+C to work**, use the wrapper script instead of running `vcm auto:start` directly:

```bash
# From anywhere (recommended)
~/.asdf/installs/nodejs/20.19.5/lib/node_modules/@vibecodingmachine/cli/scripts/auto-start-wrapper.sh

# Or if you know the package location
/path/to/vibecodingmachine/packages/cli/scripts/auto-start-wrapper.sh
```

### Creating a Convenient Alias

Add this to your `~/.zshrc` or `~/.bashrc`:

```bash
# Alias for vcm auto:start with Ctrl+C support
alias vcm-start='~/.asdf/installs/nodejs/20.19.5/lib/node_modules/@vibecodingmachine/cli/scripts/auto-start-wrapper.sh'
```

Then you can simply run:
```bash
vcm-start
vcm-start --max-chats 10
```

## Solution

This wrapper script:
1. Runs `vcm auto:start` in the background
2. Monitors keyboard input in the foreground (in the wrapper's process)
3. When Ctrl+C, Esc, or 'x' is pressed, creates a `.stop` file
4. The running `vcm` process detects the file (via watchdog timer) and exits gracefully

## Usage

### Direct Usage
```bash
# From the project root
./packages/cli/scripts/auto-start-wrapper.sh

# Or with options
./packages/cli/scripts/auto-start-wrapper.sh --max-chats 5
```

### How to Stop
- **Ctrl+C**: Stops the process (most common)
- **Esc key**: Stops the process
- **'x' key**: Stops the process
- **`vcm auto:stop`**: From another terminal

## How It Works

1. **Wrapper starts**: Spawns `node vcm auto:start` as a background process
2. **Wrapper monitors**: Uses `read -t 0.5 -n 1` to check for key presses every 500ms
3. **Key pressed**: Creates `~/.config/vibecodingmachine/.stop` file
4. **Watchdog detects**: The running vcm process has a watchdog that checks for this file every 500ms
5. **Graceful exit**: When detected, vcm kills Aider processes and exits the main loop
6. **Cleanup**: Wrapper removes the stop file and exits

## Alternative: Manual Stop File

You can also create the stop file manually:
```bash
# Create stop file
touch ~/.config/vibecodingmachine/.stop

# Or use the stop command
vcm auto:stop
```

## Technical Details

### Watchdog Timer
The watchdog runs every 500ms in the Node.js process:
```javascript
const watchdog = setInterval(async () => {
  if (await fs.pathExists(stopFilePath)) {
    console.log('🛑 Stop signal detected (via .stop file)');
    shouldExit = true;
    exitReason = 'user-stop';
    await fs.unlink(stopFilePath);
  }

  if (shouldExit) {
    clearInterval(watchdog);
    aiderManager.killAllProcesses();
    // Loop breaks naturally
  }
}, 500);
```

### Why Not Direct SIGINT?
Node.js SIGINT handlers aren't called when:
- `await` is blocked on a child process
- The event loop is not running (waiting for I/O)
- Child processes intercept signals first

The wrapper script solves this by handling signals at the shell level (outside Node.js).

## Testing

Test the wrapper with Ctrl+C:
```bash
./packages/cli/scripts/auto-start-wrapper.sh &
sleep 5
# Press Ctrl+C
# Should see: "🛑 Stop signal detected" and process exits
```

Test with stop command:
```bash
./packages/cli/scripts/auto-start-wrapper.sh &
sleep 5
vcm auto:stop
# Should see process exit within 2 seconds
```
