From f309656325143775be16bbd8caf3b79d7eb0cb0e Mon Sep 17 00:00:00 2001 From: Gustavo Madeira Santana Date: Sun, 19 Apr 2026 17:33:23 -0400 Subject: [PATCH] fix(matrix): align mention-stripped command body --- .../matrix/src/matrix/monitor/handler.test.ts | 25 +++++++++++++------ .../matrix/src/matrix/monitor/handler.ts | 11 +++++--- 2 files changed, 25 insertions(+), 11 deletions(-) diff --git a/extensions/matrix/src/matrix/monitor/handler.test.ts b/extensions/matrix/src/matrix/monitor/handler.test.ts index e23c4f15b09..0bd94931a83 100644 --- a/extensions/matrix/src/matrix/monitor/handler.test.ts +++ b/extensions/matrix/src/matrix/monitor/handler.test.ts @@ -514,14 +514,15 @@ describe("matrix monitor handler pairing account scope", () => { it("strips the Matrix self user id before room slash command detection", async () => { const hasControlCommand = vi.fn((text?: string) => text === "/new"); - const { handler, recordInboundSession } = createMatrixHandlerTestHarness({ - cfg: { commands: { useAccessGroups: false } }, - isDirectMessage: false, - mentionRegexes: [], - shouldHandleTextCommands: () => true, - hasControlCommand, - getMemberDisplayName: async () => "sender", - }); + const { handler, finalizeInboundContext, recordInboundSession } = + createMatrixHandlerTestHarness({ + cfg: { commands: { useAccessGroups: false } }, + isDirectMessage: false, + mentionRegexes: [], + shouldHandleTextCommands: () => true, + hasControlCommand, + getMemberDisplayName: async () => "sender", + }); await handler( "!room:example.org", @@ -532,6 +533,14 @@ describe("matrix monitor handler pairing account scope", () => { ); expect(hasControlCommand).toHaveBeenCalledWith("/new", expect.anything()); + expect(finalizeInboundContext).toHaveBeenCalledWith( + expect.objectContaining({ + RawBody: "@bot:example.org /new", + CommandBody: "/new", + BodyForAgent: "@bot:example.org /new", + BodyForCommands: "/new", + }), + ); expect(recordInboundSession).toHaveBeenCalled(); }); diff --git a/extensions/matrix/src/matrix/monitor/handler.ts b/extensions/matrix/src/matrix/monitor/handler.ts index c62a487b1cc..76745956ccd 100644 --- a/extensions/matrix/src/matrix/monitor/handler.ts +++ b/extensions/matrix/src/matrix/monitor/handler.ts @@ -935,8 +935,8 @@ export function createMatrixRoomMessageHandler(params: MatrixMonitorHandlerParam surface: "matrix", }); const useAccessGroups = cfg.commands?.useAccessGroups !== false; - // Keep this scoped to command precheck so mention/history/body semantics - // continue to see the original Matrix message. + // Keep mention stripping on the command-only path so history and agent + // prompt text continue to see the original Matrix message. const commandCheckText = stripMatrixMentionPrefix({ text: mentionPrecheckText, userId: selfUserId, @@ -1076,6 +1076,7 @@ export function createMatrixRoomMessageHandler(params: MatrixMonitorHandlerParam await commitInboundEventIfClaimed(); return undefined; } + const commandBodyText = hasControlCommandInMessage ? commandCheckText : bodyText; const senderName = await getSenderName(); if (_configuredBinding) { const { ensureConfiguredAcpBindingReady } = await loadAcpBindingRuntime(); @@ -1123,6 +1124,7 @@ export function createMatrixRoomMessageHandler(params: MatrixMonitorHandlerParam inboundHistory, senderName, bodyText, + commandBodyText, media, locationPayload, messageId: _messageId, @@ -1176,6 +1178,7 @@ export function createMatrixRoomMessageHandler(params: MatrixMonitorHandlerParam inboundHistory, senderName, bodyText, + commandBodyText, media, locationPayload, messageId: _messageId, @@ -1294,7 +1297,9 @@ export function createMatrixRoomMessageHandler(params: MatrixMonitorHandlerParam const ctxPayload = core.channel.reply.finalizeInboundContext({ Body: body, RawBody: bodyText, - CommandBody: bodyText, + CommandBody: commandBodyText, + BodyForAgent: bodyText, + BodyForCommands: commandBodyText, InboundHistory: inboundHistory && inboundHistory.length > 0 ? inboundHistory : undefined, From: isDirectMessage ? `matrix:${senderId}` : `matrix:channel:${roomId}`, To: `room:${roomId}`,