#5638: fix: rewrite sessionFile paths during state dir migration
commands
Cluster:
Session File Path Management
## Summary
Fixes #5631
When migrating from legacy state directories (`.clawdbot`, `.moltbot`, `.moldbot`) to `.openclaw`, the `sessionFile` paths inside `sessions.json` were not being updated. This caused the old directory to be recreated when sessions were written.
## Root Cause
`sessions.json` contained hardcoded paths like:
```json
{
"sessionFile": "/Users/username/.clawdbot/agents/main/sessions/xxx.jsonl"
}
```
The migration moved the file itself but didn't rewrite the paths inside.
## Changes
- Add `rewriteSessionFilePath()` to update legacy paths in session entries
- Detect `.clawdbot`/`.moltbot`/`.moldbot` patterns and rewrite to target dir
- Add regression test to verify path rewriting
## Testing
- Added regression test that verifies `sessionFile` paths are rewritten
- All 16 state migration tests pass
<!-- greptile_comment -->
<h2>Greptile Overview</h2>
<h3>Greptile Summary</h3>
This PR fixes legacy state-dir migrations so that `sessionFile` paths embedded in `sessions.json` are rewritten from `.clawdbot`/`.moltbot`/`.moldbot` to the new OpenClaw state directory during `migrateLegacySessions`.
Implementation adds a small helper that detects legacy state-dir substrings (cross-platform path separators) and rewrites the stored path to point at the migrated sessions directory, preventing the old legacy directory from being recreated later when sessions are persisted. A regression test exercises the migration by seeding a legacy `sessions.json` containing a hardcoded legacy path and verifying the stored entry no longer references the legacy dir.
<h3>Confidence Score: 4/5</h3>
- This PR is safe to merge with low risk; changes are localized and covered by a targeted regression test.
- The migration behavior change is narrow (rewrite of `sessionFile` only when a legacy dir pattern is present) and is applied during the existing normalization pass. The added test reduces regression risk, though it could be strengthened to assert the exact rewritten target path.
- src/commands/doctor-state-migrations.test.ts (tighten assertion to match intended target path).
<!-- greptile_other_comments_section -->
**Context used:**
- Context from `dashboard` - CLAUDE.md ([source](https://app.greptile.com/review/custom-context?memory=fd949e91-5c3a-4ab5-90a1-cbe184fd6ce8))
- Context from `dashboard` - AGENTS.md ([source](https://app.greptile.com/review/custom-context?memory=0d0c8278-ef8e-4d6c-ab21-f5527e322f13))
<!-- /greptile_comment -->
Most Similar PRs
#18593: fix: resolve symlinks in session path validation (#18553)
by EpaL · 2026-02-16
84.3%
#11602: fix(config): skip stale legacy config files when openclaw.json exists
by akoscz · 2026-02-08
83.3%
#3410: fix(sessions): always compute session paths from current environment
by sakunsylvi · 2026-01-28
80.7%
#16135: fix: handle session file paths from other agents in doctor command
by MisterGuy420 · 2026-02-14
80.6%
#17026: fix(doctor): handle invalid session file paths gracefully
by Limitless2023 · 2026-02-15
80.3%
#3561: fix: fail fast when both state dirs exist
by adityarao3 · 2026-01-28
79.3%
#15888: fix: store relative session file paths instead of absolute
by devAnon89 · 2026-02-14
79.0%
#18179: CLI: add sessions --json-debug diagnostics
by p6l-richard · 2026-02-16
78.9%
#20188: fix: Update sessionFile path when rolling to new session in cron jobs
by jriff · 2026-02-18
78.7%
#15793: fix(sessions): gracefully handle stale cross-agent session file paths
by lxcong · 2026-02-13
78.1%