#21493: fix: auto-initialize agent sessions.json on agent creation
commands
size: XS
Cluster:
Session File Path Management
When creating a new agent, the agent state directory (e.g., ~/.openclaw/agents/<agentId>/sessions/) was not initialized with a sessions.json file. This caused the agent to not appear in the web UI dropdown even after the agent was created.
Now, ensureWorkspaceAndSessions() automatically creates an empty sessions.json when initializing an agent, ensuring the agent appears in the UI selector immediately.
## Summary
Describe the problem and fix in 2–5 bullets:
- Problem:
- Why it matters:
- What changed:
- What did NOT change (scope boundary):
## Change Type (select all)
- [ ] Bug fix
- [ ] Feature
- [ ] Refactor
- [ ] Docs
- [ ] Security hardening
- [ ] Chore/infra
## Scope (select all touched areas)
- [ ] Gateway / orchestration
- [ ] Skills / tool execution
- [ ] Auth / tokens
- [ ] Memory / storage
- [ ] Integrations
- [ ] API / contracts
- [ ] UI / DX
- [ ] CI/CD / infra
## Linked Issue/PR
- Closes #
- Related #
## User-visible / Behavior Changes
List user-visible changes (including defaults/config).
If none, write `None`.
## Security Impact (required)
- New permissions/capabilities? (`Yes/No`)
- Secrets/tokens handling changed? (`Yes/No`)
- New/changed network calls? (`Yes/No`)
- Command/tool execution surface changed? (`Yes/No`)
- Data access scope changed? (`Yes/No`)
- If any `Yes`, explain risk + mitigation:
## Repro + Verification
### Environment
- OS:
- Runtime/container:
- Model/provider:
- Integration/channel (if any):
- Relevant config (redacted):
### Steps
1.
2.
3.
### Expected
-
### Actual
-
## Evidence
Attach at least one:
- [ ] Failing test/log before + passing after
- [ ] Trace/log snippets
- [ ] Screenshot/recording
- [ ] Perf numbers (if relevant)
## Human Verification (required)
What you personally verified (not just CI), and how:
- Verified scenarios:
- Edge cases checked:
- What you did **not** verify:
## Compatibility / Migration
- Backward compatible? (`Yes/No`)
- Config/env changes? (`Yes/No`)
- Migration needed? (`Yes/No`)
- If yes, exact upgrade steps:
## Failure Recovery (if this breaks)
- How to disable/revert this change quickly:
- Files/config to restore:
- Known bad symptoms reviewers should watch for:
## Risks and Mitigations
List only real risks for this PR. Add/remove entries as needed. If none, write `None`.
- Risk:
- Mitigation:
<!-- greptile_comment -->
<h3>Greptile Summary</h3>
adds auto-initialization of `sessions.json` when creating a new agent to ensure agents appear in the UI dropdown immediately after creation
**Critical Issue Found:**
- the initialization creates `{ sessions: [] }` but the session store expects an empty object `{}` (a `Record<string, SessionEntry>`). this will cause JSON parsing to succeed but the structure will be incompatible with the session loading logic in `loadSessionStore()` which expects a plain object mapping session keys to session entries
**Major changes:**
- adds new logic in `ensureWorkspaceAndSessions()` to create `~/.openclaw/agents/<agentId>/sessions/sessions.json` if it doesn't exist
- uses try/catch around `fs.stat()` to check if file exists
- creates the directory and file with initial structure when missing
<h3>Confidence Score: 1/5</h3>
- critical bug will prevent agents from appearing in UI despite initialization
- the code creates `{ sessions: [] }` but the session store expects `{}` (empty object), causing a structural mismatch. while JSON parsing won't fail, the session loading logic expects a `Record<string, SessionEntry>` and will not handle the `sessions` array property correctly. this defeats the purpose of the PR
- pay close attention to `src/commands/onboard-helpers.ts:312` - the data structure must match the session store format
<sub>Last reviewed commit: 73f0c69</sub>
<!-- greptile_other_comments_section -->
<sub>(2/5) Greptile learns from your feedback when you react with thumbs up/down!</sub>
<!-- /greptile_comment -->
Most Similar PRs
#20072: feat(sessions_spawn): add sessionKey param to reuse sub-agent sessions
by Be1Human · 2026-02-18
81.1%
#15941: fix(sessions): allow session file paths from other agents' sessions...
by LiJianLi128 · 2026-02-14
80.3%
#15744: fix: allow cross-agent session path validation
by scottgl9 · 2026-02-13
78.8%
#15888: fix: store relative session file paths instead of absolute
by devAnon89 · 2026-02-14
77.3%
#20336: fix(sessions): resolve transcriptPath using agentId when storePath ...
by Limitless2023 · 2026-02-18
77.2%
#13412: fix(sessions): refresh allowAgents permissions after gateway restart
by arun-dev-des · 2026-02-10
76.9%
#18179: CLI: add sessions --json-debug diagnostics
by p6l-richard · 2026-02-16
76.8%
#16135: fix: handle session file paths from other agents in doctor command
by MisterGuy420 · 2026-02-14
76.5%
#16171: fix: trust absolute sessionFile paths in multi-agent setups [AI-ass...
by iJaack · 2026-02-14
76.4%
#16061: fix(sessions): tolerate invalid sessionFile metadata
by haoyifan · 2026-02-14
76.4%