← Back to PRs

#13832: feat(matrix): add sessionScope and thread-scoped inbound sessions

by yamoroc open 2026-02-11 02:48 View on GitHub →
channel: matrix size: L
## 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