Skip to content

feat(matrix): thread-isolated sessions and per-chat-type threadReplies - #57995

Merged
gumadeiras merged 6 commits into
openclaw:mainfrom
teconomix:feat/matrix-thread-isolated-sessions-streaming
Mar 31, 2026
Merged

gumadeiras merged 6 commits into
openclaw:mainfrom
teconomix:feat/matrix-thread-isolated-sessions-streaming

Conversation

@teconomix

Copy link
Copy Markdown
Contributor

Summary

Two improvements to the Matrix plugin that should have been part of the streaming PR.

1. Thread-isolated sessions

Currently, when threadReplies: "always" is set, all threads in a Matrix room share the same session key. This means the agent has no way to keep context separate between different threads in the same room.

This PR adds thread-scoped session keys by calling resolveThreadSessionKeys in resolveMatrixInboundRoute. When a threadRootId is present (and differs from messageId), the session key becomes <base>:thread:<threadRootId> — giving each thread its own isolated conversation history, exactly like Mattermost's replyToMode: "all" behavior.

Binding overrides (runtime and configured ACP bindings) take priority and are unaffected.

2. Per-chat-type threadReplies

Adds dm.threadReplies config option, allowing different threadReplies values for DMs vs group rooms:

{
  "channels": {
    "matrix": {
      "threadReplies": "always",
      "dm": {
        "policy": "pairing",
        "threadReplies": "off"
      }
    }
  }
}

This lets you keep group threads isolated (each thread = separate session) while keeping DMs flat (no threading).

Changes

  • route.ts: Import resolveThreadSessionKeys; apply thread suffix to session key when thread root present
  • types.ts: Add threadReplies field to MatrixDmConfig
  • config-schema.ts: Add threadReplies to DM schema (inline expansion of buildNestedDmConfigSchema)
  • handler.ts: Add dmThreadReplies param; compute effectiveThreadReplies per-message
  • index.ts: Read dm?.threadReplies and pass to handler
  • route.test.ts: Add 3 tests for thread session key isolation
  • handler.test.ts, handler.body-for-agent.test.ts: Update expected session keys for threaded messages

Not in scope

Streaming support was already merged in #56387 and requires no further changes.

@openclaw-barnacle openclaw-barnacle Bot added channel: matrix Channel integration: matrix size: S labels Mar 30, 2026
@greptile-apps

greptile-apps Bot commented Mar 30, 2026

Copy link
Copy Markdown
Contributor

Greptile Summary

This PR adds two improvements to the Matrix plugin: (1) thread-isolated sessions so each Matrix thread gets its own session key (<base>:thread:<rootId>) rather than sharing the room-level key, and (2) a dm.threadReplies config option that lets DMs use a different thread-reply mode than group rooms.

The per-chat-type threadReplies change (handler.ts, index.ts, types.ts, config-schema.ts) is implemented cleanly and correctly. The config-schema inline expansion preserves all fields from buildNestedDmConfigSchema and adds the new field without regressions.

The main concern is in route.ts: thread session isolation is applied unconditionally, regardless of the threadReplies / dm.threadReplies setting. The PR description explicitly states that dm.threadReplies: "off" should "keep DMs flat," but the routing logic never reads that setting — so DM thread replies still receive isolated session keys even when the user opted out of threading. This creates a silent session split where the bot starts with fresh context when responding to thread replies in a DM, while the user expected a single continuous conversation. No test covers the threadReplies: "off" + threaded message combination.

Confidence Score: 4/5

  • Mostly safe, but one P1 logic gap: thread isolation fires regardless of threadReplies setting, which can silently drop DM session context when dm.threadReplies is "off".
  • The per-chat-type threadReplies feature (handler, index, types, schema) is correctly implemented. The thread isolation in route.ts is also functionally correct for the primary use case (threadReplies: "always"). However, the isolation is not gated on the effective threadReplies value, meaning dm.threadReplies: "off" does not suppress session isolation as the PR description implies. This is a present behavioral defect for users who configure that combination, not a speculative future concern.
  • extensions/matrix/src/matrix/monitor/route.ts — the thread-isolation block (lines 97–115) needs a threadReplies guard to honour dm.threadReplies: "off".
Prompt To Fix All With AI
This is a comment left during a code review.
Path: extensions/matrix/src/matrix/monitor/route.ts
Line: 97-115

Comment:
**Thread isolation ignores `threadReplies` / `dm.threadReplies` setting**

The PR description says `dm.threadReplies: "off"` is intended to "keep DMs flat (no threading)," but the thread-isolation logic here fires unconditionally whenever `threadRootId` differs from `messageId` — it never checks the effective `threadReplies` setting. As a result, even when a user configures `dm.threadReplies: "off"`, any DM message that arrives inside a Matrix thread will receive a thread-scoped session key (`<base>:thread:<rootId>`), silently splitting the conversation history from the non-threaded messages in the same DM.

Concretely:
- `$root` (sent as the thread root) → `messageId == threadRootId` → no isolation → session `agent:…:main`
- `$reply1` (in the same thread) → `messageId != threadRootId` → **isolated** → session `agent:…:main:thread:$root`

So the bot responds without threading (correct, per `dm.threadReplies: "off"`) but with a fresh session that lacks the context of the root message (unintended).

`resolveMatrixInboundRoute` would need to accept the effective `threadReplies` value (or an explicit `isolateThreadSessions` flag) and skip the thread-key block when threading is disabled.  There are also no test cases covering `dm.threadReplies: "off"` combined with thread messages, so this gap would go undetected.

```ts
// Proposed guard (sketch):
const effectiveThreadReplies =
  params.isDirectMessage && params.dmThreadReplies !== undefined
    ? params.dmThreadReplies
    : params.threadReplies;

if (!configuredBinding && !configuredSessionKey && threadId && effectiveThreadReplies !== "off") {
```

How can I resolve this? If you propose a fix, please make it concise.

Reviews (1): Last reviewed commit: "feat(matrix): thread-isolated sessions a..." | Re-trigger Greptile

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 4657e4dc7b

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

@gumadeiras gumadeiras self-assigned this Mar 31, 2026
@openclaw-barnacle openclaw-barnacle Bot added docs Improvements or additions to documentation size: M and removed size: S labels Mar 31, 2026

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 550e9ae755

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 7de917034c

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: d69939ae98

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@gumadeiras
gumadeiras force-pushed the feat/matrix-thread-isolated-sessions-streaming branch from d69939a to f3e75c7 Compare March 31, 2026 02:41
@openclaw-barnacle openclaw-barnacle Bot added the agents Agent runtime and tooling label Mar 31, 2026
teconomix and others added 6 commits March 30, 2026 22:42
- Append 🧵<threadRootId> to session key when a Matrix thread root
  is present, giving each thread its own isolated session (mirrors
  Mattermost replyToMode=all behavior)
- Add dm.threadReplies config option to override threadReplies for DMs
  independently of group rooms
- Update handler tests to expect thread-scoped session keys
- Add route.test.ts coverage for thread session key isolation

Streaming support was already merged upstream in PR openclaw#56387.
@gumadeiras
gumadeiras force-pushed the feat/matrix-thread-isolated-sessions-streaming branch from f3e75c7 to 9ed96dd Compare March 31, 2026 02:44
@gumadeiras
gumadeiras merged commit 697dddb into openclaw:main Mar 31, 2026
8 checks passed
@gumadeiras

Copy link
Copy Markdown
Member

Merged via squash.

Thanks @teconomix!

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 9ed96dd063

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@teconomix

Copy link
Copy Markdown
Contributor Author

Thank you @gumadeiras

pgondhi987 pushed a commit to pgondhi987/openclaw that referenced this pull request Mar 31, 2026
openclaw#57995)

Merged via squash.

Prepared head SHA: 9ed96dd
Co-authored-by: teconomix <[email protected]>
Co-authored-by: gumadeiras <[email protected]>
Reviewed-by: @gumadeiras
pgondhi987 pushed a commit to pgondhi987/openclaw that referenced this pull request Mar 31, 2026
openclaw#57995)

Merged via squash.

Prepared head SHA: 9ed96dd
Co-authored-by: teconomix <[email protected]>
Co-authored-by: gumadeiras <[email protected]>
Reviewed-by: @gumadeiras
lovewanwan pushed a commit to lovewanwan/openclaw that referenced this pull request Apr 28, 2026
openclaw#57995)

Merged via squash.

Prepared head SHA: 9ed96dd
Co-authored-by: teconomix <[email protected]>
Co-authored-by: gumadeiras <[email protected]>
Reviewed-by: @gumadeiras
ogt-redknie pushed a commit to ogt-redknie/OPENX that referenced this pull request May 2, 2026
openclaw#57995)

Merged via squash.

Prepared head SHA: 9ed96dd
Co-authored-by: teconomix <[email protected]>
Co-authored-by: gumadeiras <[email protected]>
Reviewed-by: @gumadeiras
github-actions Bot pushed a commit to Desicool/openclaw that referenced this pull request May 24, 2026
openclaw#57995)

Merged via squash.

Prepared head SHA: 9ed96dd
Co-authored-by: teconomix <[email protected]>
Co-authored-by: gumadeiras <[email protected]>
Reviewed-by: @gumadeiras
Nachx639 pushed a commit to Nachx639/clawdbot that referenced this pull request Jun 17, 2026
openclaw#57995)

Merged via squash.

Prepared head SHA: 9ed96dd
Co-authored-by: teconomix <[email protected]>
Co-authored-by: gumadeiras <[email protected]>
Reviewed-by: @gumadeiras
vixco pushed a commit to vixco/openclaw that referenced this pull request Aug 16, 2026
openclaw#57995)

Merged via squash.

Prepared head SHA: 9ed96dd
Co-authored-by: teconomix <[email protected]>
Co-authored-by: gumadeiras <[email protected]>
Reviewed-by: @gumadeiras
globalcaos pushed a commit to globalcaos/tinkerclaw that referenced this pull request Sep 29, 2026
openclaw#57995)

Merged via squash.

Prepared head SHA: 9ed96dd
Co-authored-by: teconomix <[email protected]>
Co-authored-by: gumadeiras <[email protected]>
Reviewed-by: @gumadeiras
lhfer pushed a commit to lhfer/openclaw that referenced this pull request Oct 11, 2026
openclaw#57995)

Merged via squash.

Prepared head SHA: 9ed96dd
Co-authored-by: teconomix <[email protected]>
Co-authored-by: gumadeiras <[email protected]>
Reviewed-by: @gumadeiras
obviyus added a commit that referenced this pull request Oct 31, 2026
#177272)

fix(matrix): start new threads with their own history, not the room transcript

A new ordinary Matrix thread requested a fork of the whole room transcript, so the first thread turn sent the room's earlier conversation to the model. That broke the isolated thread history established by #57995. New threads now start from their own history. Thread root context, model inheritance, binding selection and the room's own timeline are unchanged. There is no SDK change.

Evidence: built Gateway with a real Matrix server. On main, the first turn in a new thread included the room history in the model input (2 of 2). With this change, new threads were isolated (2 of 2). The room timeline, existing-thread history, root context, model inheritance and binding controls behaved the same on both refs. Details: #177272 (comment).

Thanks @Linux2010.

Co-authored-by: Ayaan Zaidi <[email protected]>
github-actions Bot pushed a commit to Desicool/openclaw that referenced this pull request Nov 1, 2026
openclaw#177272)

fix(matrix): start new threads with their own history, not the room transcript

A new ordinary Matrix thread requested a fork of the whole room transcript, so the first thread turn sent the room's earlier conversation to the model. That broke the isolated thread history established by openclaw#57995. New threads now start from their own history. Thread root context, model inheritance, binding selection and the room's own timeline are unchanged. There is no SDK change.

Evidence: built Gateway with a real Matrix server. On main, the first turn in a new thread included the room history in the model input (2 of 2). With this change, new threads were isolated (2 of 2). The room timeline, existing-thread history, root context, model inheritance and binding controls behaved the same on both refs. Details: openclaw#177272 (comment).

Thanks @Linux2010.

Co-authored-by: Ayaan Zaidi <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

agents Agent runtime and tooling channel: matrix Channel integration: matrix docs Improvements or additions to documentation size: L

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants