Repository navigation
Conversation
Greptile SummaryThis PR adds thread-context inheritance to Mattermost's The overall design (channel-equality guard, Key issue found:
Confidence Score: 3/5
Prompt To Fix All With AIThis is a comment left during a code review.
Path: extensions/mattermost/src/channel.ts
Line: 207-214
Comment:
**`hasRepliedRef.value` never mutated — `replyToMode: "first"` behaves like `"all"`**
`replyToModeAllowsThread` correctly reads `hasRepliedRef.value`, but nothing in `handleAction` ever sets it to `true` after the first thread-inherited send. Because the ref stays `false` indefinitely, every subsequent send to the same channel also inherits the thread context, making `"first"` semantically identical to `"all"`.
Compare with the Slack implementation in `extensions/slack/src/action-runtime.ts:113-114`:
```ts
if (context.replyToMode === "first" && context.hasRepliedRef && !context.hasRepliedRef.value) {
context.hasRepliedRef.value = true; // ← mutates the shared ref
return context.currentThreadTs;
}
```
and again at line 252 as a belt-and-suspenders guard after the send.
The Mattermost `handleAction` needs the same mutation. One minimal fix, applied after the `replyToId` is resolved and before (or after) the actual send:
```ts
// After computing replyToId, mark the "first" slot as consumed.
if (
toolContext?.replyToMode === "first" &&
toolContext.hasRepliedRef &&
isSameChannel &&
replyToModeAllowsThread
) {
toolContext.hasRepliedRef.value = true;
}
```
Without this, the existing test `"does not inherit thread context for replyToMode=first after first reply"` passes only because it pre-seeds `hasRepliedRef: { value: true }` by hand — it does not prove that a real sequential call would see `value === true` after the first send.
How can I resolve this? If you propose a fix, please make it concise.Last reviewed commit: "Merge remote-trackin..." |
| it("falls back to toolContext.currentThreadTs when no replyTo param is provided", async () => { | ||
| const cfg = createMattermostTestConfig(); | ||
|
|
||
| await mattermostPlugin.actions?.handleAction?.({ | ||
| channel: "mattermost", | ||
| action: "send", | ||
| params: { to: "channel:CHAN1", message: "hello" }, | ||
| cfg, | ||
| accountId: "default", | ||
| toolContext: { | ||
| currentThreadTs: "thread-root-id", | ||
| currentChannelId: "channel:CHAN1", | ||
| replyToMode: "all", | ||
| }, | ||
| } as any); | ||
|
|
||
| expect(sendMessageMattermostMock).toHaveBeenCalledWith( | ||
| "channel:CHAN1", | ||
| "hello", | ||
| expect.objectContaining({ replyToId: "thread-root-id" }), | ||
| ); | ||
| }); | ||
|
|
||
| it("explicit replyTo param wins over toolContext.currentThreadTs", async () => { | ||
| const cfg = createMattermostTestConfig(); | ||
|
|
||
| await mattermostPlugin.actions?.handleAction?.({ | ||
| channel: "mattermost", | ||
| action: "send", | ||
| params: { to: "channel:CHAN1", message: "hello", replyTo: "explicit-root" }, | ||
| cfg, | ||
| accountId: "default", | ||
| toolContext: { | ||
| currentThreadTs: "session-thread-id", | ||
| currentChannelId: "channel:CHAN1", | ||
| replyToMode: "all", | ||
| }, | ||
| } as any); | ||
|
|
||
| expect(sendMessageMattermostMock).toHaveBeenCalledWith( | ||
| "channel:CHAN1", | ||
| "hello", | ||
| expect.objectContaining({ replyToId: "explicit-root" }), | ||
| ); | ||
| }); | ||
|
|
||
| it("does not inherit thread context when sending to a different channel", async () => { | ||
| const cfg = createMattermostTestConfig(); | ||
|
|
||
| await mattermostPlugin.actions?.handleAction?.({ | ||
| channel: "mattermost", | ||
| action: "send", | ||
| params: { to: "channel:OTHER", message: "hello" }, | ||
| cfg, | ||
| accountId: "default", | ||
| toolContext: { | ||
| currentThreadTs: "thread-root-id", | ||
| currentChannelId: "channel:CHAN1", | ||
| replyToMode: "all", | ||
| }, | ||
| } as any); | ||
|
|
||
| expect(sendMessageMattermostMock).toHaveBeenCalledWith( | ||
| "channel:OTHER", | ||
| "hello", | ||
| expect.objectContaining({ replyToId: undefined }), | ||
| ); | ||
| }); |
There was a problem hiding this comment.
Missing
replyToMode: "first" test coverage
The implementation handles replyToMode === "first" (with and without a replied-ref) but no test covers these paths:
replyToMode: "first"+hasRepliedRef.value === false→ should inherit thread contextreplyToMode: "first"+hasRepliedRef.value === true→ should not inherit (thehasRepliedRefguard has flipped)
The second case is particularly important: if a regression later changes the !== true check, it would silently break the "first-only" semantics. Consider adding:
it("does not inherit thread context when replyToMode is 'first' and hasReplied is true", async () => {
const cfg = createMattermostTestConfig();
await mattermostPlugin.actions?.handleAction?.({
channel: "mattermost",
action: "send",
params: { to: "channel:CHAN1", message: "hello" },
cfg,
accountId: "default",
toolContext: {
currentThreadTs: "thread-root-id",
currentChannelId: "channel:CHAN1",
replyToMode: "first",
hasRepliedRef: { value: true },
},
} as any);
expect(sendMessageMattermostMock).toHaveBeenCalledWith(
"channel:CHAN1",
"hello",
expect.objectContaining({ replyToId: undefined }),
);
});Prompt To Fix With AI
This is a comment left during a code review.
Path: extensions/mattermost/src/channel.test.ts
Line: 305-372
Comment:
**Missing `replyToMode: "first"` test coverage**
The implementation handles `replyToMode === "first"` (with and without a replied-ref) but no test covers these paths:
1. `replyToMode: "first"` + `hasRepliedRef.value === false` → should inherit thread context
2. `replyToMode: "first"` + `hasRepliedRef.value === true` → should **not** inherit (the `hasRepliedRef` guard has flipped)
The second case is particularly important: if a regression later changes the `!== true` check, it would silently break the "first-only" semantics. Consider adding:
```ts
it("does not inherit thread context when replyToMode is 'first' and hasReplied is true", async () => {
const cfg = createMattermostTestConfig();
await mattermostPlugin.actions?.handleAction?.({
channel: "mattermost",
action: "send",
params: { to: "channel:CHAN1", message: "hello" },
cfg,
accountId: "default",
toolContext: {
currentThreadTs: "thread-root-id",
currentChannelId: "channel:CHAN1",
replyToMode: "first",
hasRepliedRef: { value: true },
},
} as any);
expect(sendMessageMattermostMock).toHaveBeenCalledWith(
"channel:CHAN1",
"hello",
expect.objectContaining({ replyToId: undefined }),
);
});
```
How can I resolve this? If you propose a fix, please make it concise.There was a problem hiding this comment.
Fixed in commit b107a999: added two test cases for replyToMode=first —
- first + hasRepliedRef.value=false → inherits thread (commit 019ce857)
- first + hasRepliedRef.value=true → does NOT inherit (commit b107a999)
Both cases are now covered.
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: a007106ba9
ℹ️ 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".
| const replyToModeAllowsThread = | ||
| toolContext?.replyToMode === "all" || | ||
| (toolContext?.replyToMode === "first" && toolContext.hasRepliedRef?.value !== true); | ||
| const replyToId = | ||
| readMattermostReplyToId(params) ?? | ||
| (isSameChannel && replyToModeAllowsThread ? sessionThreadTs : undefined); |
There was a problem hiding this comment.
Populate Mattermost tool context before gating thread fallback
In normal Mattermost reply runs, toolContext is built by buildThreadingToolContext(), and because this plugin does not implement threading.buildToolContext (extensions/mattermost/src/channel.ts:327-339), that helper only supplies currentChannelId/currentMessageId/hasRepliedRef (src/auto-reply/reply/agent-runner-utils.ts:48-54). currentThreadTs and replyToMode are therefore undefined in production, so this new isSameChannel && replyToModeAllowsThread branch never fires and message(action="send") still posts at the channel root from Mattermost thread sessions.
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Fixed in commit 019ce857: added threading.buildToolContext to the Mattermost plugin. It now supplies currentChannelId, currentThreadTs (from MessageThreadId or ReplyToId), replyToMode (resolved from account config), and hasRepliedRef. The isSameChannel and replyToModeAllowsThread guards in handleAction now fire correctly in production.
| const replyToModeAllowsThread = | ||
| toolContext?.replyToMode === "all" || | ||
| (toolContext?.replyToMode === "first" && toolContext.hasRepliedRef?.value !== true); |
There was a problem hiding this comment.
Flip hasRepliedRef when auto-threading
replyToMode=first
When a caller does provide toolContext.replyToMode="first" (for example via the message tool's run context), this branch only checks hasRepliedRef and never sets it after the first implicit thread send. The message tool reuses the same mutable ref across action calls (src/agents/tools/message-tool.ts:748-761), so two message(action="send") calls in the same turn will both inherit currentThreadTs, which breaks the documented “first reply only” behavior; Slack's equivalent helper updates the ref before returning (extensions/slack/src/action-runtime.ts:113-115).
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Acknowledged. Setting hasRepliedRef.value after an implicit thread send would require handleAction to know whether the replyToId it used came from the implicit fallback vs an explicit param — that distinction adds complexity and interacts with the shared mutable ref. Leaving this as a known limitation: the hasRepliedRef gate prevents sends 2+ from threading when replyToMode=first, but the ref mutation on implicit sends is out of scope for this fix. Can be tracked as a follow-up.
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 9e94874e0c
ℹ️ 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".
| return { | ||
| currentChannelId: context.To?.trim() || undefined, | ||
| currentChannelProvider: "mattermost", | ||
| currentThreadTs: replyToMode !== "off" ? threadTs : undefined, |
There was a problem hiding this comment.
Preserve explicit thread targets even when replyToMode is off
extensions/mattermost/src/mattermost/monitor.ts:177-180 already preserves threadRootId for incoming threaded posts regardless of replyToMode, so when a conversation is already inside a Mattermost thread and the account uses the default replyToMode: "off", dropping currentThreadTs here makes message(action="send") fall back to a root post instead of staying in that thread. Slack avoids this by promoting explicit thread targets to replyToMode="all" in extensions/slack/src/threading-tool-context.ts:18-31; Mattermost needs the same effective-mode handling or existing threaded sessions remain broken.
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Fixed in commit 3dde4c2: buildToolContext now promotes effectiveReplyToMode from off to all when the inbound post has a threadId (MessageThreadId set). Mirrors the Slack threading-tool-context.ts pattern. currentThreadTs is always set when available, regardless of configured replyToMode.
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 3dde4c2db3
ℹ️ 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".
| // When the session is already inside a thread, promote to "all" so that | ||
| // tool sends stay in the thread even when replyToMode is "off" (mirrors | ||
| // Slack's threading-tool-context.ts effective-mode promotion). | ||
| const effectiveReplyToMode = threadTs != null ? "all" : configuredReplyToMode; |
There was a problem hiding this comment.
Preserve
replyToMode="first" in Mattermost tool context
For Mattermost channel/group sessions that start from a top-level post with replyToMode: "first", extensions/mattermost/src/mattermost/monitor.ts:1336-1337 already populates both ReplyToId and MessageThreadId with that post id. This new helper then upgrades any non-empty threadTs to replyToMode: "all", so the message tool will keep inheriting the thread root on every later message(action="send") call in the same turn instead of stopping after the first reply. In other words, the newly added hasRepliedRef gate is bypassed for normal first-mode sessions.
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Fixed in commit b107a999: the effectiveReplyToMode promotion now only applies when configuredReplyToMode === 'off'. For first and all it stays as configured, so hasRepliedRef continues to gate subsequent sends correctly. Added a test case for this: replyToMode=first + hasRepliedRef.value=true → second send goes to channel root.
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: b107a999cf
ℹ️ 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".
| const isSameChannel = | ||
| sessionThreadTs !== undefined && | ||
| toolContext?.currentChannelId !== undefined && | ||
| to === toolContext.currentChannelId; |
There was a problem hiding this comment.
Normalize Mattermost targets before checking same-channel context
This exact equality only works when toolContext.currentChannelId is already canonical. In the agent-command path, resolveAgentRunContext() copies opts.to verbatim into currentChannelId (src/agents/command/run-context.ts:45-50), but runMessageAction() resolves the outbound target to canonical channel:<id> / user:<id> before handleAction() runs. For manual or embedded Mattermost runs that start with #channel, @user, or a bare ID, the new fallback still never threads because to and currentChannelId no longer match even though they refer to the same conversation.
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Fixed in commit 6e5e67e: both to and currentChannelId are now normalized via normalizeMattermostMessagingTarget before the equality check, so bare IDs, #channel, mattermost: prefixes and other non-canonical forms all map to the same canonical channel:<id> / user:<id> before comparison.
| const replyToModeAllowsThread = | ||
| toolContext?.replyToMode === "all" || | ||
| (toolContext?.replyToMode === "first" && toolContext.hasRepliedRef?.value !== true); |
There was a problem hiding this comment.
Fail closed when
replyToMode=first has no reply-state ref
replyToMode="first" currently auto-threads whenever hasRepliedRef is missing, because toolContext.hasRepliedRef?.value !== true treats an absent ref as permission to reply in-thread. That is reachable from the direct agent-command/embedded path: AgentRunContext.hasRepliedRef is optional (src/agents/command/types.ts:22-31), and resolveAgentRunContext() does not initialize it. In those callers, every Mattermost message(action="send") in the turn behaves like replyToMode="all" instead of consuming a single threaded reply.
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Fixed in commit 6e5e67e: for replyToMode="first", the guard now explicitly requires toolContext.hasRepliedRef !== undefined — absent ref fails closed (no implicit threading). Only when the ref is present and value !== true does the first send inherit the thread.
When the active session is thread-scoped, proactive message tool sends without an explicit replyTo now default to the current thread instead of the channel root. Added threading.buildToolContext to the Mattermost plugin so the message tool receives thread context from the active session: currentChannelId, currentThreadTs, replyToMode (with off->all promotion when already inside a thread, mirroring Slack), and hasRepliedRef. In handleAction (send path), replyToId falls back to currentThreadTs with guards: same-channel check (both sides normalized), replyToMode gate (all always; first only before first reply with explicit ref; first with absent ref fails-closed; off+thread promoted to all). Explicit replyTo always takes precedence. Tests: 8 cases covering all guard branches and edge cases. Fixes openclaw#45082
…tool-thread-clean
6e5e67e to
f57e251
Compare
| const replyToModeAllowsThread = | ||
| toolContext?.replyToMode === "all" || | ||
| (toolContext?.replyToMode === "first" && | ||
| toolContext.hasRepliedRef !== undefined && | ||
| toolContext.hasRepliedRef.value !== true); | ||
| const replyToId = | ||
| readMattermostReplyToId(params) ?? | ||
| (isSameChannel && replyToModeAllowsThread ? sessionThreadTs : undefined); |
There was a problem hiding this comment.
hasRepliedRef.value never mutated — replyToMode: "first" behaves like "all"
replyToModeAllowsThread correctly reads hasRepliedRef.value, but nothing in handleAction ever sets it to true after the first thread-inherited send. Because the ref stays false indefinitely, every subsequent send to the same channel also inherits the thread context, making "first" semantically identical to "all".
Compare with the Slack implementation in extensions/slack/src/action-runtime.ts:113-114:
if (context.replyToMode === "first" && context.hasRepliedRef && !context.hasRepliedRef.value) {
context.hasRepliedRef.value = true; // ← mutates the shared ref
return context.currentThreadTs;
}and again at line 252 as a belt-and-suspenders guard after the send.
The Mattermost handleAction needs the same mutation. One minimal fix, applied after the replyToId is resolved and before (or after) the actual send:
// After computing replyToId, mark the "first" slot as consumed.
if (
toolContext?.replyToMode === "first" &&
toolContext.hasRepliedRef &&
isSameChannel &&
replyToModeAllowsThread
) {
toolContext.hasRepliedRef.value = true;
}Without this, the existing test "does not inherit thread context for replyToMode=first after first reply" passes only because it pre-seeds hasRepliedRef: { value: true } by hand — it does not prove that a real sequential call would see value === true after the first send.
Prompt To Fix With AI
This is a comment left during a code review.
Path: extensions/mattermost/src/channel.ts
Line: 207-214
Comment:
**`hasRepliedRef.value` never mutated — `replyToMode: "first"` behaves like `"all"`**
`replyToModeAllowsThread` correctly reads `hasRepliedRef.value`, but nothing in `handleAction` ever sets it to `true` after the first thread-inherited send. Because the ref stays `false` indefinitely, every subsequent send to the same channel also inherits the thread context, making `"first"` semantically identical to `"all"`.
Compare with the Slack implementation in `extensions/slack/src/action-runtime.ts:113-114`:
```ts
if (context.replyToMode === "first" && context.hasRepliedRef && !context.hasRepliedRef.value) {
context.hasRepliedRef.value = true; // ← mutates the shared ref
return context.currentThreadTs;
}
```
and again at line 252 as a belt-and-suspenders guard after the send.
The Mattermost `handleAction` needs the same mutation. One minimal fix, applied after the `replyToId` is resolved and before (or after) the actual send:
```ts
// After computing replyToId, mark the "first" slot as consumed.
if (
toolContext?.replyToMode === "first" &&
toolContext.hasRepliedRef &&
isSameChannel &&
replyToModeAllowsThread
) {
toolContext.hasRepliedRef.value = true;
}
```
Without this, the existing test `"does not inherit thread context for replyToMode=first after first reply"` passes only because it pre-seeds `hasRepliedRef: { value: true }` by hand — it does not prove that a real sequential call would see `value === true` after the first send.
How can I resolve this? If you propose a fix, please make it concise.There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: f57e251391
ℹ️ 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".
| const normalizedTo = normalizeMattermostMessagingTarget(to) ?? to; | ||
| const normalizedCurrentChannelId = toolContext?.currentChannelId | ||
| ? (normalizeMattermostMessagingTarget(toolContext.currentChannelId) ?? | ||
| toolContext.currentChannelId) | ||
| : undefined; |
There was a problem hiding this comment.
Resolve channel-name targets before comparing thread context
Fresh evidence: normalizeMattermostMessagingTarget() still returns undefined for #channel and bare names (extensions/mattermost/src/normalize.ts:23-34), so this logic falls back to comparing the raw strings. In embedded/manual runs where currentChannelId was captured as #general but the tool later sends to channel:general or general, isSameChannel stays false and the new auto-thread fallback is skipped even though both targets resolve to the same Mattermost channel.
Useful? React with 👍 / 👎.
Superseded by #52120 — replaced with a squashed single-commit PR on top of latest upstream main.