#13057: feat(matrix): add sessionScope=room to route sessions by roomId
docs
channel: matrix
stale
size: S
Cluster:
Matrix Room Management Enhancements
## Summary
Adds `channels.matrix.sessionScope` (`"legacy"` | `"room"`) to control how Matrix inbound messages are routed to OpenClaw sessions.
- `"legacy"` (default): preserve existing behavior
- direct/DM-like rooms route by **senderId**
- rooms route by **roomId**
- `"room"`: route *all* inbound Matrix messages by **roomId**, ensuring **one session per Matrix room** (including DM-like 2-member rooms)
Includes a small unit test for route peer selection and updates Matrix docs + changelog.
## Motivation
Matrix “DM-like” rooms (including 2-member encrypted rooms) can be marked as direct via account data (`m.direct` / `is_direct`). When that happens, routing by senderId collapses multiple rooms into a single DM session, which is undesirable for setups that want strict per-room isolation.
This change decouples *session routing* from *DM detection* by making the routing scope configurable.
Note: this works for encrypted rooms too — encryption affects message decryption, not the session routing identifiers.
## Changes
- **Matrix config schema:** add `channels.matrix.sessionScope`
- **Routing:** introduce `resolveMatrixRoutePeer()` helper and use it when calling `resolveAgentRoute()`
- **Thread isolation:** unchanged; continues to use `:thread:<threadRootId>` suffix as upstream now does
- **Diagnostics:** add a **verbose-only** log line showing resolved `routePeer` (helps debug routing without spamming normal logs)
- **Docs:** document the new option in `docs/channels/matrix.md`
- **Tests:** add `extensions/matrix/src/matrix/monitor/route-peer.test.ts`
## Testing
- `corepack pnpm -w vitest run extensions/matrix/src` (Matrix extension unit tests; 12 files / 30 tests)
- `corepack pnpm -w lint extensions/matrix`
## AI assistance
- AI-assisted: yes (OpenClaw/Codex)
- I reviewed/understand the change: it only affects how the Matrix handler chooses the `peer.kind/id` passed into `resolveAgentRoute` when `channels.matrix.sessionScope="room"`. Default behavior remains unchanged.
<!-- greptile_comment -->
<h2>Greptile Overview</h2>
<h3>Greptile Summary</h3>
Adds a new Matrix configuration option `channels.matrix.sessionScope` to control how inbound Matrix messages map to OpenClaw sessions. A helper (`resolveMatrixRoutePeer`) selects the `peer.kind/id` passed to `core.channel.routing.resolveAgentRoute`, enabling a `"room"` mode that routes all messages by `roomId` (one session per Matrix room) while keeping the existing `"legacy"` behavior as default. Includes a small unit test and updates Matrix docs and the Matrix plugin changelog.
<h3>Confidence Score: 4/5</h3>
- This PR looks safe to merge after addressing small correctness/docs issues around config handling.
- The routing change is isolated and covered by a focused unit test, and default behavior remains unchanged. Main concern is the force-cast of user config to `MatrixSessionScope`, which can hide misconfiguration, plus a small docs omission in the config reference section.
- extensions/matrix/src/matrix/monitor/handler.ts; docs/channels/matrix.md
<!-- 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
#13832: feat(matrix): add sessionScope and thread-scoped inbound sessions
by yamoroc · 2026-02-11
86.1%
#13451: feat(matrix): add forceRoomRouting to bypass DM detection for allow...
by yamoroc · 2026-02-10
83.4%
#11869: feat: Multi-account Matrix plugin with accountId routing
by jacoblyles · 2026-02-08
79.4%
#10606: fix(matrix): keep room IDs without :server suffix as-is during reso...
by majorminors · 2026-02-06
78.2%
#9106: fix(matrix): override DM detection for rooms in groups config
by robertcorreiro · 2026-02-04
77.8%
#7868: Default DM sessions to per-channel scope (avoid webchat contention)
by Smile232323 · 2026-02-03
77.6%
#20078: feat(session): Add channelGroups config(optional config) for shared...
by demarlik01 · 2026-02-18
77.4%
#18718: matrix: add pending group history context for room messages
by pharasyte · 2026-02-17
76.6%
#23333: fix(matrix): add accountId routing for multi-account message sending 🤖
by BadTurki · 2026-02-22
76.3%
#9051: fix(sessions): respect dmScope config in CLI agent commands
by benleavett · 2026-02-04
76.2%