npm package: @monolex/pty-daemon
Description: PTY daemon helper for Monolex terminal functionality
Author: Monolex Team
Latest version: 0.10.1

# @monolex/pty-daemon

PTY daemon helper for Monolex terminal functionality. This package provides a standalone PTY daemon that can be used by the Monolex app to create terminal sessions even when the main app's spawn-helper has Hardened Runtime restrictions.

## Installation

```bash
npm install -g @monolex/pty-daemon
```

### What's New in v0.10.0
- **Pre-built binaries for macOS arm64** - No Python or Xcode required!
- **Automatic Hardened Runtime removal** - Works in production apps
- **Faster installation** - Skip compilation on supported platforms

The postinstall script will automatically:
1. Use pre-built binaries if available (macOS arm64)
2. Otherwise compile node-pty for your system
3. Fix spawn-helper permissions and signature
4. Find and save Electron path for daemon usage
5. Test the spawn-helper functionality

## How It Works

This daemon runs as a separate process using Electron's Node.js runtime (`ELECTRON_RUN_AS_NODE=1`) and provides PTY functionality through Unix domain sockets.

### Architecture

```
Monolex App (Production)
    ↓
  IPC Handler
    ↓
  Daemon Process (this package)
    ├─ node-pty (without Hardened Runtime)
    ├─ Unix domain sockets
    └─ PTY sessions
```

## Usage

### Automatic (via Monolex app)

The Monolex app will automatically detect and use this daemon when installed globally.

### Manual Start

```bash
# With Electron (recommended)
ELECTRON_RUN_AS_NODE=1 /path/to/electron monolex-pty-daemon

# With system Node.js (if ABI compatible)
monolex-pty-daemon
```

### Control Commands

The daemon exposes a control socket at `~/.monolex/sockets/daemon-control.sock` for management:

- `create-session` - Create a new PTY session
- `remove-session` - Remove a PTY session
- `list-sessions` - List all active sessions
- `resize-session` - Resize terminal dimensions
- `status` - Get daemon status
- `shutdown` - Gracefully shut down daemon

## Configuration

Create a config file and pass it to the daemon:

```json
{
  "sessionPrefix": "daemon",
  "maxSessions": 10,
  "defaultShell": "bash",
  "defaultCols": 120,
  "defaultRows": 40,
  "socketDir": "~/.monolex/sockets",
  "logFile": "/tmp/monolex-pty-daemon.log"
}
```

```bash
monolex-pty-daemon --config /path/to/config.json
```

## Troubleshooting

### spawn-helper Issues

If you see `posix_spawnp failed` errors:

1. Check spawn-helper signature:
```bash
codesign -dvvv node_modules/node-pty/build/Release/spawn-helper
```

2. If it has "Runtime Version", the postinstall script should fix it automatically.

3. Manual fix if needed:
```bash
# Remove Hardened Runtime
codesign --remove-signature node_modules/node-pty/build/Release/spawn-helper
codesign --force --sign - node_modules/node-pty/build/Release/spawn-helper
```

### Logs

Check daemon logs at:
- `/tmp/monolex-pty-daemon.log` - Daemon runtime logs
- `/tmp/pty-daemon.log` - PTY process logs

### Testing

Run the test script:
```bash
npm test
```

This will verify that spawn-helper can create PTY processes correctly.

## Requirements

- macOS, Linux, or Windows
- Node.js 18+ or Electron 20+
- Build tools (only if pre-built binaries not available):
  - macOS: `xcode-select --install` (not needed for Apple Silicon)
  - Linux: `apt-get install build-essential`
  - Windows: `npm install -g windows-build-tools`

## License

MIT

## Support

Report issues at: https://github.com/monolex/pty-daemon/issues