← Back to PRs

#13057: feat(matrix): add sessionScope=room to route sessions by roomId

by spengrah open 2026-02-10 02:45 View on GitHub →
docs channel: matrix stale size: S
## 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