# Snap Node Compatibility Issue

## Problem

When using Officer with **snap node** (`/snap/bin/node`), the Pi harness fails with the following error:

```
[Pi] [INFO] Pi process exited
code=1
```

This occurs on the first chat message, before Pi even processes the command.

## Root Cause

The snap version of Node.js has an incompatibility with how Bun's `spawn()` function sets up piped stdin file descriptors. When Officer tries to spawn a Pi process with `stdin: 'pipe'`, the process immediately exits with code 1, preventing the RPC communication from working.

This does **NOT** happen with:
- System node installed via apt/package manager
- NodeSource node
- Homebrew node (on macOS)
- Any non-snap Node.js installation

## Solution

**Uninstall snap node and install a system-managed version instead:**

### Step 1: Remove snap node

```bash
sudo snap remove node
```

### Step 2: Install Node.js via apt (recommended)

```bash
# Add NodeSource repository for Node 20 LTS
curl -fsSL https://deb.nodesource.com/setup_20.x | sudo -E bash -

# Install Node.js
sudo apt-get install -y nodejs

# Verify installation
node --version
which node  # Should be /usr/bin/node (NOT /snap/bin/node)
```

### Alternative: Using system apt repository

If NodeSource is unavailable in your region:

```bash
sudo apt-get update
sudo apt-get install -y nodejs npm
```

### Alternative: Using nvm (Node Version Manager)

For more control over Node.js versions:

```bash
# Install nvm
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.0/install.sh | bash

# Install Node LTS
nvm install --lts
nvm use --lts

# Verify
which node  # Should be ~/.nvm/versions/node/*/bin/node
```

## Verification

After installing a non-snap Node.js:

```bash
# Verify node is not from snap
which node
# Output should NOT contain "/snap/"

# Verify node works
node --version

# Clear npm cache
npm cache clean --force
npm install -g pi-coding-agent
```

Then restart Officer and try the chat functionality again - it should work!

## Troubleshooting

### Still seeing the error after reinstalling Node?

1. **Restart Officer service** (if running as a service):
   ```bash
   sudo systemctl restart officer
   # or
   bun dev  # if running locally
   ```

2. **Verify Bun can find the correct node**:
   ```bash
   bun run "which node"
   which node
   # Both should show the same path, not /snap/bin/node
   ```

3. **Check Pi installation**:
   ```bash
   pi --version
   pi --list-models
   ```

### Error logs to look for

If you still see the error, check Officer logs for:

```
Pi process exited with code 1
nodeVersion: ...
nodeExePath: /snap/bin/node
isSnapNode: true
```

This confirms snap node is the issue.

## Why snap node has this issue

The snap package environment isolates certain system calls and file descriptor handling, which conflicts with Bun's pipe setup mechanism. The snap version of Node.js doesn't properly inherit file descriptor flags when pipes are created by Bun, causing the process to fail on startup.

This is a known incompatibility and not a bug in Officer or Pi itself.
