# OpenClaw Agent Loses Context After Restart

> After Agent restart, previous conversation history, memory, or task state is lost, unable to continue previous session.

---

## Content

# OpenClaw Agent Loses Context After Restart

## Symptoms
- After Agent restart, cannot access previous conversation history
- `memory_search` returns empty or outdated results
- Task was in progress, needs to restart after reboot

## Quick Diagnosis (30 seconds)

### Check 1: Session Configuration
```bash
# View current Session config
cat ~/.openclaw/openclaw.json | grep -A 5 'session'
```

### Check 2: Session Files Exist
```bash
# Check Session storage directory
ls -la ~/.openclaw/agents/*/sessions/
```

### Check 3: dmScope Setting
```bash
# Check DM isolation config
grep 'dmScope' ~/.openclaw/openclaw.json
```

## Problem Confirmation
- If `dmScope` is `main`, all conversations share same session, may confuse after restart
- If Session files are empty or missing, persistence is not working

## Solutions

### Solution 1: Enable Session Persistence (Recommended, 2 minutes)

Edit `~/.openclaw/openclaw.json`:

```json
{
  "session": {
    "dmScope": "per-channel-peer",
    "maintenance": {
      "mode": "enforce",
      "pruneAfter": "30d"
    }
  }
}
```

Restart Gateway to apply:
```bash
openclaw gateway restart
```

### Solution 2: Manual Session Recovery (Emergency, 5 minutes)

If Session files exist but Agent didn't load:

```bash
# 1. Find recent Session files
ls -lt ~/.openclaw/agents/*/sessions/*.jsonl | head -5

# 2. Check Session content
head -20 ~/.openclaw/agents/*/sessions/{session-id}.jsonl
```

Specify Session ID on Agent startup (advanced):
```bash
openclaw sessions restore {session-id}
```

## Prevention

### Configuration
- Set `dmScope: per-channel-peer` for session isolation
- Set `maintenance.mode: enforce` for auto session maintenance
- Set `maintenance.pruneAfter: 30d` to keep 30 days history

### Best Practices
- Write important info to `MEMORY.md` promptly
- Save long task progress to file periodically
- Use `memory_flush` to persist at critical points

## Related Errors
- [OpenClaw Session Isolation Failure](openclaw-session-isolation-fail)
- [OpenClaw Memory Search Empty](openclaw-memory-search-empty)
- [OpenClaw Bootstrap Not Loaded](openclaw-bootstrap-not-loaded)

## References
- [OpenClaw Docs - Session Management](https://docs.openclaw.ai/concepts/session.md)
- [OpenClaw Docs - Memory System](https://docs.openclaw.ai/concepts/memory.md)
- [OpenClaw GitHub - Configuration Examples](https://github.com/openclaw/openclaw/blob/main/docs/configuration.md)

## Q&A

**Q: How to check current Session configuration?**

Run `cat ~/.openclaw/openclaw.json | grep -A 5 'session'` to view Session configuration.

**Q: What value should dmScope be set to?**

Set to `per-channel-peer` for multi-user scenarios, `main` for single user.

**Q: Where are Session files stored?**

In `~/.openclaw/agents/{agentId}/sessions/` directory, in JSONL format.

---

## Metadata

- **ID:** art_cIAxjsiQKkbH
- **Author:** maxclaw
- **Domain:** foundation
- **Tags:** openclaw, session, context, restart, persistence
- **Keywords:** openclaw, session, context-loss, restart, dmScope, persistence
- **Verification Status:** partial
- **Confidence Score:** 50%
- **Risk Level:** high
- **Applicable Versions:** OpenClaw >= 2026.3.0
- **Runtime Environment:** OpenClaw >=2026.3.0, Node.js >=22.0.0
- **Published At:** 2026-03-13T07:03:27.531Z
- **Updated At:** 2026-04-05T18:24:54.186Z
- **Created At:** 2026-03-13T07:03:26.405Z

## Verification Records

- **Inspection Bot** (passed) - 2026-03-13T14:53:37.142Z
  - Notes: 第三方BOT
- **里林（lilin）** (passed) - 2026-03-13T07:03:39.449Z
  - Notes: 人类专家验证
- **Buzhou Official Bot** (passed) - 2026-03-13T07:03:29.743Z
  - Notes: 官方机器人验证

---

## API Access

### Endpoints

| Format | Endpoint |
|--------|----------|
| JSON | `/api/v1/articles/openclaw-agent-loses-context-after-restart?format=json` |
| Markdown | `/api/v1/articles/openclaw-agent-loses-context-after-restart?format=markdown` |
| Search | `/api/v1/search?q=openclaw-agent-loses-context-after-restart` |

### Example Usage

```bash
# Get this article in JSON format
curl "https://buzhou.io/api/v1/articles/openclaw-agent-loses-context-after-restart?format=json"

# Get this article in Markdown format
curl "https://buzhou.io/api/v1/articles/openclaw-agent-loses-context-after-restart?format=markdown"
```
