#13832: feat(matrix): add sessionScope and thread-scoped inbound sessions
channel: matrix
size: L
Cluster:
Matrix Room Management Enhancements
## Summary
This PR re-submits and finalizes the Matrix `sessionScope` + thread-isolation work from the previously closed PR #13515.
It adds configurable inbound session scoping (`room` vs `agent`) and introduces deterministic thread-scoped session keys for Matrix room threads, so concurrent thread conversations do not bleed context across each other.
## What changed
### 1) `sessionScope` support (Matrix inbound)
- Added `channels.matrix.sessionScope` schema + typings:
- `room` (default): per-room session isolation.
- `agent`: single shared Matrix session per agent (`agent:{agentId}:matrix:main`).
### 2) Thread isolation
- Added `resolveMatrixSessionKey(...)` to centralize Matrix session-key derivation.
- For non-DM room thread traffic with a thread root, inbound events now resolve to:
- `SessionKey = {baseSessionKey}:thread:{threadRootId}`
- `ParentSessionKey = {baseSessionKey}`
- DM traffic remains non-thread-scoped to avoid unintended thread partitioning in direct chats.
### 3) Inbound pipeline wiring
- Applied resolved session keys consistently in:
- envelope/context finalization,
- inbound session persistence,
- session metadata timestamps,
- system-event enqueueing.
### 4) Changelog
- Updated `extensions/matrix/CHANGELOG.md` with a new `2026.2.9` entry documenting:
- `sessionScope` behavior,
- thread isolation design,
- test coverage additions.
## Validation
- Added focused unit tests for config and session-key resolution:
- `extensions/matrix/src/config-schema.test.ts`
- `extensions/matrix/src/matrix/monitor/handler.test.ts`
- Verified expected behavior for:
- default `room` scope,
- explicit `agent` scope,
- room-thread key isolation + parent linkage,
- DM non-thread behavior.
### Test run
```bash
pnpm vitest run extensions/matrix
```
Result: **13 test files passed, 35 tests passed**.
## Risk / rollout notes
- Backward compatibility is preserved by default (`sessionScope` defaults to `room`).
- `agent` mode is opt-in and only affects inbound Matrix session grouping.
- Thread isolation only applies to room-thread traffic; DMs continue using direct session keys.
## References
- Closed predecessor: #13515
<!-- greptile_comment -->
<h2>Greptile Overview</h2>
<h3>Greptile Summary</h3>
This PR introduces configurable Matrix inbound session scoping (`channels.matrix.sessionScope`: `room` vs `agent`) and centralizes session-key derivation via `resolveMatrixSessionKey()`. It also adds deterministic thread-scoped session keys for non-DM room threads (with `ParentSessionKey` linking back to the base session), wiring these resolved keys through inbound context creation, session persistence, and system-event enqueueing. Unit tests were added to validate the new schema and session-key resolution behavior.
<h3>Confidence Score: 3/5</h3>
- This PR is close to mergeable but has two behavior issues that can cause incorrect session behavior in production.
- Thread session persistence currently gets skipped on thread-root fetch errors, which guarantees repeated root fetch attempts and prevents sessions from ever being recorded for those threads. Additionally, `sessionScope=agent` collapses all Matrix DMs into a single shared agent session key, which can cause cross-DM context bleed unless explicitly intended and documented.
- extensions/matrix/src/matrix/monitor/handler.ts
<!-- greptile_other_comments_section -->
<!-- /greptile_comment -->
## AI-assisted disclosure
- [x] Mark as AI-assisted in the PR title or description
- [x] Note the degree of testing (lightly tested)
- [x] Include prompts or session logs if possible
- [x] Confirm you understand what the code does
**Testing degree:** Lightly tested (targeted Matrix unit tests via `pnpm vitest run extensions/matrix`).
**Prompt/session log (summary):**
- "Implement Matrix `sessionScope` and room-thread isolation with backward-compatible defaults."
- "Add focused tests for config schema and session-key resolution, then verify target test suite passes."
**Confirmation:** I reviewed the final diff and understand the behavior changes and trade-offs in this PR.
Most Similar PRs
#13057: feat(matrix): add sessionScope=room to route sessions by roomId
by spengrah · 2026-02-10
86.1%
#13451: feat(matrix): add forceRoomRouting to bypass DM detection for allow...
by yamoroc · 2026-02-10
80.2%
#18718: matrix: add pending group history context for room messages
by pharasyte · 2026-02-17
74.9%
#20078: feat(session): Add channelGroups config(optional config) for shared...
by demarlik01 · 2026-02-18
73.7%
#13026: matrix: optionally include media metadata in message.read
by yamoroc · 2026-02-10
73.6%
#21105: feat(matrix): multi-account support
by omnibot1985 · 2026-02-19
72.9%
#19403: feat(slack): add dm.threadSession option for per-message thread ses...
by Vasiliy-Bondarenko · 2026-02-17
72.6%
#23333: fix(matrix): add accountId routing for multi-account message sending 🤖
by BadTurki · 2026-02-22
72.2%
#11869: feat: Multi-account Matrix plugin with accountId routing
by jacoblyles · 2026-02-08
71.9%
#19388: Fix #19278: Case-insensitive Matrix room ID matching
by cedillarack · 2026-02-17
71.4%