# Configuration

`@edgeai/edge-client-ts` reads configuration from **two** sources,
merged with this precedence (highest → lowest):

1. **Environment variables** (preferred for secrets — never commit them).
2. **`config.toml`** (or `config.json`) in the platform-specific config
   directory.
3. Built-in defaults.

> **Precedence example:** if `EDGE_AUTH_TOKEN` is set in the environment
> AND `relay.auth_token` is set in `config.toml`, the **environment
> variable wins**.

---

## Config file location

| OS | Path |
|----|------|
| Linux | `~/.config/edge-ai-agent/config.toml` |
| macOS | `~/Library/Application Support/edge-ai-agent/config.toml` |
| Windows | `%APPDATA%\edge-ai-agent\config.toml` |

A JSON `config.json` is also accepted as a fallback (TOML takes
precedence if both exist).

---

## Full schema

This schema is **identical** to the Rust `edge-client`'s `config.toml`.
Both clients read the same fields; mismatches are a bug.

```toml
# A stable identifier for this machine. Auto-generated on first run if
# omitted; do NOT change after enrolling (the relay binds your operator
# to a specific machine_id).
machine_id = "uuid-v4-or-fleet-name"

# Optional: pre-bind this client to an operator (otherwise set via
# `ec-ts enroll`).
owner_operator_id = "op_<your-operator-id>"

[server]
# WebSocket URL of the relay's /edge endpoint.
ws_url = "wss://relay.example.com/edge"

# Optional mode-specific URLs (Batch 5 — live network-mode switching).
# When the operator toggles network_mode='lan'/'wan' from the dashboard,
# the daemon transparently reconnects to the matching URL.
# ws_url_lan = "ws://192.168.1.10:3001/edge"
# ws_url_wan = "wss://wan-relay.example.com/edge"

[agent]
# Default agent id used when the dashboard does not pin one.
default_agent_id = "worker_default"

[buffer]
# Maximum in-memory queue size in MB before spill-to-disk is triggered.
max_memory_mb = 50

# How often (ms) the daemon flushes in-memory entries to the WS sender.
flush_interval_ms = 100

# How long (ms) an in-flight entry may wait for an ACK before being
# re-sent.
ack_timeout_ms = 5000

# Number of re-send attempts before an entry is dumped to encrypted disk
# (anti-data-loss — see docs/PROTOCOL.md).
max_retries = 3

# Buffer dump encryption key. Hex, 64 chars = 32 bytes.
# PREFER the EDGE_BUFFER_KEY env var (do not commit to source).
# key = "<64 hex chars>"

[logging]
# Log level: trace | debug | info | warn | error
level = "info"

# Local log directory (relative to config dir or absolute).
local_log_dir = "logs"

[shell]
# Enable the /shell-edge binary channel. Default OFF — opt in per node.
enabled = false

# Default TTY size when the dashboard opens a fresh shell.
default_cols = 80
default_rows = 24

[relay]
# Fleet shared secret for WS auth. PREFER the EDGE_AUTH_TOKEN env var.
# auth_token = "<your-fleet-shared-secret>"

# Optional: TLS SPKI fingerprint pin (SHA-256 hex of the
# SubjectPublicKeyInfo). Strongly recommended for production.
fingerprint = "<sha256-hex>"

[resources]
# Soft memory cap (MB). The daemon dumps the buffer to disk above this.
memory_max_mb = 2048

# Soft CPU quota (%). The daemon sheds load above this percentage.
cpu_quota_percent = 50

# Maximum concurrent tasks (sessions + RPCs).
tasks_max = 256

[cmd_gate]
# AGT-02 command gate mode. off | warn | block. Default: block.
mode = "block"

# Per-command timeout (seconds). Default: 60.
timeout_s = 60
```

---

## Environment variables

> **Treat all `EDGE_*` env vars as secrets.** Never commit them. Use your
> platform's secret manager (e.g. systemd `EnvironmentFile=` with mode
> `0600`, macOS launchd `EnvironmentVariables`, Windows Credential
> Manager, or a `.env` file with `chmod 600`).

| Variable | Equivalent config | Notes |
|----------|-------------------|-------|
| `EDGE_AUTH_TOKEN` | `relay.auth_token` | Fleet shared secret. **Required** in production. |
| `EDGE_BUFFER_KEY` | `buffer.key` | Hex, 64 chars. Required for offline buffer encryption. |
| `EDGE_BUFFER_KEY_PREVIOUS` | — | Hex, 64 chars. Used during key rotation. |
| `EDGE_RELAY_FINGERPRINT` | `relay.fingerprint` | Overrides config; recommended for prod. |
| `EDGE_OWNER_OPERATOR_ID` | `owner_operator_id` | Overrides enrolled operator. |
| `EDGEAI_REQUIRE_TLS` | — | `1` forces `wss://` outside production. |
| `NODE_ENV=production` | — | Implicitly forces `wss://`. |
| `EDGE_CMD_GATE_MODE` | `cmd_gate.mode` | `off` / `warn` / `block`. |
| `EDGE_FAIL_FAST` | — | Fail-fast policy toggle (`0`/`1`). |

---

## Secret handling

- **Never** paste tokens, API keys, or fingerprints into:
  - `config.toml` in a git repository
  - shell history (use a password manager or `read -s`)
  - Slack / chat / issue trackers
- **Always prefer env vars** for any value labeled "secret" or "token"
  in the schema above.
- **Rotate** the fleet shared secret via the dashboard; the daemon
  reconnects within the next heartbeat interval (~30 s).
- **Revoke** lost tokens in the dashboard immediately. The relay enforces
  revocation on the next WS upgrade (no waiting for the daemon to
  notice).

---

## Verifying your config

```bash
# Validate without running the daemon
ec-ts status --once

# Show effective config (with secrets redacted)
ec-ts whoami --verbose
```

See [`docs/TROUBLESHOOTING.md`](TROUBLESHOOTING.md) for common
configuration mistakes.