#9051: fix(sessions): respect dmScope config in CLI agent commands
commands
stale
# Summary
- Fix CLI agent commands (`openclaw agent --to`) ignoring `session.dmScope` config
- Add `channel` parameter to `resolveSessionKeyForRequest()` and `resolveSession()`
- Use `buildAgentSessionKey()` (dmScope-aware) instead of `resolveSessionKey()` for DM routing
## Problem
When running `openclaw agent --to <phone> --channel whatsapp`, the session key was always
`agent:main:main` regardless of `session.dmScope` config. The CLI code path used
`resolveSessionKey()` which ignores `dmScope`, while inbound webhook routing correctly used
`buildAgentSessionKey()`.
## Test plan
- [x] Set `session.dmScope: per-channel-peer` in config
- [x] Run `openclaw agent --to +1234567890 --channel whatsapp -m "test"`
- [x] Verify session creates as `agent:main:whatsapp:dm:+1234567890` (not `agent:main:main`)
🤖 AI-assisted (Claude). Code manually reviewed and understood.
<!-- greptile_comment -->
<h2>Greptile Overview</h2>
<h3>Greptile Summary</h3>
This PR updates the CLI agent command path to pass an explicit `channel` into session resolution so DM routing can respect `session.dmScope` (aligning CLI behavior with inbound webhook routing via `buildAgentSessionKey`). It threads `channel` through `resolveSessionKeyForRequest()`/`resolveSession()` and updates the gateway-based CLI command to compute the normalized channel before deriving the session key.
The main behavior change is in `src/commands/agent/session.ts`, where DM session keys are now built using `buildAgentSessionKey()` when both `--to` and `--channel` are provided, instead of the legacy `resolveSessionKey()` which ignores `dmScope`.
<h3>Confidence Score: 3/5</h3>
- This PR is close to mergeable but has a correctness issue in session key construction for non-default agents.
- The dmScope-aware routing change is targeted, but the new code path uses an agent id derived from the (absent) explicit session key, which can cause sessions to be created under the wrong agent when `--agent` is used without an explicit session key. Fixing that should make the change low risk.
- src/commands/agent/session.ts
<!-- greptile_other_comments_section -->
<sub>(2/5) Greptile learns from your feedback when you react with thumbs up/down!</sub>
**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
#7868: Default DM sessions to per-channel scope (avoid webchat contention)
by Smile232323 · 2026-02-03
85.3%
#11659: fix: Clarify session.dmScope is global in security audit warning
by veast · 2026-02-08
83.4%
#16685: Fix cli agents/approvals/discord routing edge cases
by craftowen · 2026-02-15
82.3%
#6850: fix: support direct channel:account:peer format in session key extr...
by toboto · 2026-02-02
81.1%
#15176: fix(sessions): allow channel-routed session IDs and cross-agent paths
by cathrynlavery · 2026-02-13
80.6%
#20078: feat(session): Add channelGroups config(optional config) for shared...
by demarlik01 · 2026-02-18
80.6%
#15744: fix: allow cross-agent session path validation
by scottgl9 · 2026-02-13
80.3%
#15852: fix: pass agentId when resolving IRC session paths
by MisterGuy420 · 2026-02-14
79.8%
#15792: fix: pass agentId to resolveSessionFilePath in additional call sites
by MisterGuy420 · 2026-02-13
79.4%
#8095: fix(sessions): include accountId in deliveryContext for outbound pe...
by codeslayer44 · 2026-02-03
79.4%