# MailSubsystem Core Command Reference

Single binary:

```bash
mailsubsystem <command> [options]
```

## Sync

| Command | Description |
| --- | --- |
| `check` | Verify DB and IMAP connectivity |
| `sync` | Full envelope sync and durable body sync |
| `sync-slow` | Drain body queue for rows missing `body_text` |
| `status` | Show `imap_folders` and `emails` table state |
| `sync-incremental` | Incremental sync using MODSEQ changes |
| `sync-incremental --daemon` | Run incremental sync in a loop |
| `sync-window [--days N] [--full]` | Sync envelopes for a time window |
| `daemon` | Incremental sync plus periodic full and slow sync |

## Analyze

| Command | Description |
| --- | --- |
| `analyze [--force] [message_id]` | Batch or single-email AI analysis |
| `test-llm --local` | Test local LLM connection |
| `test-llm --frontier` | Test frontier provider connection |
| `show <message_id>` | Print stored AI fields for an email |
| `process-frontier-queue [--limit N]` | Re-analyze queued emails with frontier |
| `embed-backfill [--limit N]` | Generate missing embeddings |

## Locate And File

| Command | Description |
| --- | --- |
| `resolve-orphans` | Fix rows with `location=NULL` |
| `locate [--force] [message_id]` | Generate folder recommendation |
| `file [--dry-run]` | Apply recommendations with IMAP MOVE and folder creation |

## Maintenance

| Command | Description |
| --- | --- |
| `backfill-message-tokens` | Backfill approximate token counts |
| `backfill-message <message_id>` | Fetch one message from IMAP and backfill fields |
| `backfill-from-imap [--limit N]` | Backfill missing message fields from IMAP |

## Configuration Groups

- IMAP: `ICLOUD_IMAP_SERVER`, `ICLOUD_USERNAME`, `ICLOUD_PASSWORD`
- Database: `DATABASE_URL`, `DB_HOST`, `DB_NAME`
- AI providers: `AI_PROVIDER`, `LOCAL_LLM_URL`, `GEMINI_API_KEY`, `OPENAI_API_KEY`, `ANTHROPIC_API_KEY`
- Daemon and batches: `INCREMENTAL_SYNC_INTERVAL_SECS`, `ANALYZE_LIMIT`, `LOCATE_LIMIT`

