> For the complete documentation index, see [llms.txt](https://www.openclawbook.xyz/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://www.openclawbook.xyz/en/ch38-monitoring-and-troubleshooting/38.4-troubleshooting.md). Agents can also search and read this documentation through its [MCP server](https://www.openclawbook.xyz/~gitbook/mcp).

# 38.4 Troubleshooting

> **Generated by**: Claude Opus 4.6 (initial draft) / Claude Opus 5 (`v2026.3.9` revision) **Token usage**: Input \~280k tokens, Output \~5k tokens (this section) **Source baseline**: `v2026.3.9` (`v2026.3.8-170-g665f67726`)

***

Even carefully designed systems encounter problems. OpenClaw's `doctor` command provides a one-stop automated diagnostic and repair tool that covers a wide range of scenarios from configuration errors to daemon failures.

## 38.4.1 Doctor Command Architecture

`openclaw doctor` (`src/commands/doctor.ts`) is a modular diagnostic framework consisting of 20+ specialized check modules:

```
openclaw doctor
    │
    ├── doctor-update.ts         — Version update check
    ├── doctor-ui.ts             — UI protocol version compatibility
    ├── doctor-install.ts        — Source installation issue detection
    ├── doctor-platform-notes.ts — Platform-specific issues (macOS launchd override, etc.)
    ├── doctor-config-flow.ts    — Configuration file migration and repair
    ├── doctor-auth.ts           — Authentication configuration (OAuth, API keys)
    ├── doctor-legacy-config.ts  — Legacy configuration format detection
    ├── doctor-state-integrity.ts — State directory integrity
    ├── doctor-state-migrations.ts — State data migration
    ├── doctor-sandbox.ts        — Sandbox image check
    ├── doctor-security.ts       — Security audit
    ├── doctor-gateway-services.ts — Gateway service configuration
    ├── doctor-gateway-daemon-flow.ts — Daemon management
    ├── doctor-gateway-health.ts — Gateway health probe
    ├── doctor-workspace.ts      — Workspace check
    ├── doctor-workspace-status.ts — Workspace status
    ├── doctor-completion.ts     — Shell autocompletion
    └── doctor-prompter.ts       — Interactive prompter
```

### Execution Flow

The order of Doctor's checks is carefully designed—from most basic to most advanced, with each step potentially fixing prerequisites for subsequent steps:

1. **Version check**: Whether updates are available, optional auto-update
2. **UI compatibility**: Whether console UI version matches backend
3. **Installation check**: Whether source installation is complete
4. **Platform detection**: macOS Launch Agent override, environment variables, etc.
5. **Configuration migration**: Upgrade legacy configuration format
6. **Authentication repair**: Gateway token/password configuration
7. **OAuth repair**: Anthropic OAuth profile migration
8. **State migration**: Upgrade legacy state directory
9. **State integrity**: Check for corrupted session files
10. **Sandbox check**: Whether Docker images exist and are up-to-date
11. **Service configuration**: launchd/systemd service files
12. **Security audit**: Sensitive configuration checks
13. **Model configuration**: Hooks model reference validity
14. **Gateway health**: Active probe of running Gateway
15. **Daemon**: Start/restart Gateway service
16. **Workspace**: Memory system recommendations
17. **Shell completion**: Configure bash/zsh autocompletion

### Two Running Modes

```bash
openclaw doctor          # Interactive mode: prompt step-by-step whether to fix
openclaw doctor --fix    # Auto-fix mode: skip confirmation and fix directly
```

Interactive mode uses `@clack/prompts` to provide a beautiful terminal UI, asking the user whether to fix each issue found. The `--fix` mode (`nonInteractive: true`) automatically agrees to all repair suggestions, suitable for CI/CD environments.

## 38.4.2 Key Check Items Explained

### Gateway Authentication Repair

Doctor checks whether Gateway authentication configuration is secure:

```typescript
const auth = resolveGatewayAuth({
  authConfig: cfg.gateway?.auth,
  tailscaleMode: cfg.gateway?.tailscale?.mode ?? "off",
});
const needsToken = auth.mode !== "password" && 
  (auth.mode !== "token" || !auth.token);

if (needsToken) {
  // Prompt to generate random token
  const nextToken = randomToken();  // Generate secure random token
  cfg.gateway.auth = { mode: "token", token: nextToken };
}
```

Even if the Gateway is bound to loopback, it is recommended to enable token authentication—this prevents other processes on the same machine from accessing the Gateway without authorization.

### Security Audit

`doctor-security.ts` checks for multiple security risks:

* Gateway exposed to the public internet without authentication
* Console UI exposed through reverse proxy but `trustedProxies` not configured
* Tailscale Funnel without password authentication
* Sensitive environment variables leaked

### Sandbox Image Check

`doctor-sandbox.ts` verifies the availability of Docker sandbox images:

* Check whether `openclaw/sandbox:latest` exists
* If not, prompt to pull
* Check whether the image is too old, recommend update
* Check consistency of sandbox scope configuration

### Daemon Management

`doctor-gateway-daemon-flow.ts` handles Gateway service startup and restart:

* Detect the service manager for the current platform (macOS: launchd, Linux: systemd)
* Verify that service files exist and are configured correctly
* If Gateway is not running, prompt to start
* If configuration has changed, prompt to restart

## 38.4.3 Logging System

OpenClaw uses a hierarchical logging system:

### Log Levels

| Level   | Purpose               | Enable Method        |
| ------- | --------------------- | -------------------- |
| error   | Errors and exceptions | Always enabled       |
| warn    | Warning messages      | Always enabled       |
| info    | General information   | Always enabled       |
| debug   | Debug information     | `OPENCLAW_VERBOSE=1` |
| verbose | Detailed tracing      | `OPENCLAW_VERBOSE=1` |

### Child Loggers

The Gateway creates independent child loggers for different subsystems, making it easy to filter by module:

```typescript
// src/gateway/server.impl.ts
const logTailscale = log.child("tailscale");
const logBrowser = log.child("browser");
const logDiscovery = log.child("discovery");
const logHealth = log.child("health");
```

### Common Problem Diagnosis

| Symptom                            | Possible Cause                   | Diagnostic Command                            |
| ---------------------------------- | -------------------------------- | --------------------------------------------- |
| Gateway fails to start             | Port in use                      | `lsof -i :18789`                              |
| WebSocket connection fails         | Authentication error             | `openclaw doctor`                             |
| Chat unresponsive                  | Invalid API key                  | `openclaw health --verbose`                   |
| Channel messages not sent/received | Channel not configured/linked    | `openclaw health`                             |
| No Gateway after macOS startup     | launchd configuration missing    | `launchctl list \| grep openclaw`             |
| No Gateway after Linux reboot      | Linger not enabled               | `loginctl show-user $USER \| grep Linger`     |
| Sandbox command timeout            | Docker not running/image missing | `docker ps` + `openclaw doctor`               |
| UI loads blank                     | UI resource version mismatch     | `openclaw doctor` (check UI protocol version) |

### Debug Environment Variables

| Variable                  | Purpose                        |
| ------------------------- | ------------------------------ |
| `OPENCLAW_VERBOSE=1`      | Enable verbose logging         |
| `OPENCLAW_DEBUG_HEALTH=1` | Health check debug output      |
| `OPENCLAW_DEBUG=1`        | Generic debug mode             |
| `DEBUG=openclaw:*`        | Node.js debug module filtering |

## 38.4.4 State Recovery

When OpenClaw's state data (`~/.openclaw/`) is corrupted, Doctor provides a recovery path:

1. **State integrity check** (`doctor-state-integrity.ts`): Scan session files and detect corrupted JSON
2. **State migration** (`doctor-state-migrations.ts`): Migrate data from legacy directories (`.clawdbot`, `.moltbot`)
3. **Workspace backup prompt** (`doctor-workspace-status.ts`): Recommend users to regularly backup workspace
4. **Configuration fallback**: If `openclaw.json` is corrupted, Doctor attempts to recover from `.bak` backup

***

## Section Summary

1. **`openclaw doctor`** is a modular diagnostic framework with 20+ check modules, executed in dependency order from basic to advanced, supporting both interactive and auto-fix modes.
2. **Key checks** cover authentication security, sandbox images, daemons, service configuration, state integrity, and other dimensions, with each check providing automatic repair suggestions.
3. **Logging system** uses hierarchical design, supporting child loggers with per-module filtering, environment variables control debug level.
4. **State recovery** provides a complete path to recover from corrupted data: integrity scan → legacy migration → backup recovery.
5. When encountering issues that cannot be automatically fixed, Doctor outputs specific manual repair commands (such as `openclaw config set ...`), lowering the threshold for user troubleshooting.
