# Troubleshooting

A field guide for the most common `@edgeai/edge-client-ts` errors and how
to fix them.

> **Security note:** when filing an issue or asking for support, **never
> share your `EDGE_AUTH_TOKEN`, `EDGE_BUFFER_KEY`, `EDGE_RELAY_FINGERPRINT`,
> or operator API key.** Redact everything matching `<secret>=…` in logs
> before pasting.

---

## Install / `npm install -g` fails

### `EACCES: permission denied`

You ran `npm install -g` without sudo. Either:

- Re-run with `sudo`:
  ```bash
  sudo npm install -g @edgeai/edge-client-ts
  ```
- Or fix npm's global prefix (recommended):
  ```bash
  mkdir -p ~/.npm-global
  npm config set prefix '~/.npm-global'
  export PATH="$HOME/.npm-global/bin:$PATH"
  # Persist in ~/.bashrc / ~/.zshrc
  npm install -g @edgeai/edge-client-ts
  ```

### `node-pty` build fails (no prebuilt binary)

Most platforms have a prebuilt `.node` artifact. If yours does not:

- **Linux**: `apt install build-essential python3` (Debian/Ubuntu) or
  `dnf install gcc-c++ make python3` (Fedora).
- **macOS**: `xcode-select --install`.
- **Windows**: install [windows-build-tools](https://github.com/felixrieseberg/windows-build-tools)
  or use the Visual Studio Build Tools.

### `ec-ts: command not found` after install

The global `bin` dir is not on `PATH`. Find it with:

```bash
npm config get prefix
# Add "$prefix/bin" to PATH.
```

---

## Runtime / the daemon won't start

### `WebSocket upgrade rejected (no detail)`

The relay rejected the upgrade. Common causes:

| Cause | Fix |
|-------|-----|
| Missing or wrong `EDGE_AUTH_TOKEN` | Re-export the env var, restart the daemon. |
| TLS pin mismatch (`relay_fingerprint`) | Re-extract the SPKI hash with the `openssl` one-liner in the upstream docs. |
| Relay is `wss://` but you configured `ws://` (or vice versa) | Match the scheme; set `EDGEAI_REQUIRE_TLS=1` to force `wss://`. |
| Operator revoked in the dashboard | Re-enroll with a new key (`ec-ts enroll --key <new-key>`). |

### `EAI_AGAIN` / `ENOTFOUND` (DNS resolution)

The relay hostname does not resolve. Check:

```bash
# Replace "relay.example.com" with your ws_url host
nslookup relay.example.com
curl -v https://relay.example.com/health
```

If you are behind a corporate proxy, set `HTTPS_PROXY` (and `NO_PROXY`
for the local relay) per Node's conventions.

### `Buffer full: 16777276 bytes would exceed cap of 16777216 bytes`

The in-memory queue hit `buffer.max_memory_mb`. Increase it, or speed up
flushes:

```toml
[buffer]
max_memory_mb = 64     # default 50; raise to 128+ on busy nodes
flush_interval_ms = 50 # default 100; lower for faster drain
```

Also check that `EDGE_BUFFER_KEY` is set — without it, the daemon cannot
dump the overflow to encrypted disk and starts dropping entries.

---

## Connection / WS flaps

### Daemon connects, then immediately disconnects

Look in the daemon log (`local_log_dir` from config). Two frequent
patterns:

- **`displaced by duplicate machine_id registration`** — another
  daemon (TypeScript or Rust) is already registered with the same
  `machine_id`. Kill the duplicate or set a unique `machine_id` per
  daemon.
- **`401 Unauthorized`** — fleet shared secret mismatch. Verify
  `EDGE_AUTH_TOKEN` matches the relay's `RELAY_AUTH_TOKEN`.

### `WebSocket protocol error: Handshake not finished`

The relay closed the upgrade mid-handshake. Most common cause: an
intermediating proxy (nginx, Cloudflare Tunnel) is stripping the
`Sec-WebSocket-Protocol` subprotocol header. The `edgeai-v2` protocol
identifier carries the auth token, so if the proxy strips it the relay
sees an unauthenticated request.

**Fix (Cloudflare Tunnel):** ensure the tunnel preserves headers. With
`cloudflared` defaults the `Sec-WebSocket-Protocol` header IS forwarded
to the upstream; double-check your `config.yml` `originRequest` block.

---

## `ec-ts enroll` problems

### `keychain unavailable` on Linux

`ec-ts` requires `libsecret` to store the per-operator API key. Install:

```bash
sudo apt install libsecret-1-0 libsecret-1-dev   # Debian/Ubuntu
sudo dnf install libsecret libsecret-devel        # Fedora
```

Then make sure a Secret Service provider is running (GNOME Keyring,
KDE Wallet, KeePassXC with Secret Service integration, etc.).

### `enroll` succeeds but `run` still says "no operator"

The keychain item may not be visible to the daemon process. Check:

```bash
# Linux
secret-tool search service edge-ai-agent

# macOS
security find-generic-password -s edge-ai-agent
```

If the item is missing, re-enroll with the same key.

---

## Performance / memory

- **`memory_max_mb`** is a SOFT cap. The daemon dumps to disk above it,
  not OOM-kills.
- **`tasks_max`** caps concurrent sessions + RPCs. Raise it on
  machines running many parallel agents.
- For long-lived nodes with intermittent connectivity, also raise
  `buffer.max_memory_mb` and `buffer.max_retries`.

---

## Getting more help

1. Run `ec-ts status --once` and capture the output (redact secrets).
2. Tail the daemon log (`local_log_dir` from your config).
3. File an issue at <https://github.com/baphuongna/edge-ai-agent/issues>
   with:
   - `ec-ts --version` and `node --version`
   - OS / architecture
   - Redacted `ec-ts whoami --verbose` output
   - The relevant daemon log lines

The maintainers will not ask for your `EDGE_AUTH_TOKEN` or operator API
key — if you accidentally paste one, **rotate it immediately** in the
dashboard.