Skip to content

Commit b13993a

Browse files
authored
fix: dedupe replies against their resolved delivery thread (#130510)
* fix: dedupe replies against their resolved delivery thread Compare message-tool deliveries with the channel-owned automatic reply transport before suppressing duplicate output. Preserve explicit thread targets and the existing behavior of channels without a transport hook. Related: #120339. * test: type the resolved reply transport fixture * test: preserve Slack thread fallback in follow-up fixture
1 parent d89de5c commit b13993a

4 files changed

Lines changed: 104 additions & 17 deletions

File tree

‎src/auto-reply/reply/agent-runner-payloads.test.ts‎

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -97,16 +97,20 @@ describe("buildReplyPayloads media filter integration", () => {
9797
resolveReplyTransport: ({
9898
threadId,
9999
replyToId,
100+
replyToIsExplicit,
100101
replyDelivery,
101-
}: ResolveReplyTransportParams) => ({
102-
replyToId:
103-
replyDelivery?.replyToMode === "off"
104-
? threadId != null
105-
? String(threadId)
106-
: undefined
107-
: (replyToId ?? (threadId != null ? String(threadId) : undefined)),
108-
threadId: null,
109-
}),
102+
}: ResolveReplyTransportParams) => {
103+
const allowedReply = replyDelivery?.replyToMode === "off" ? undefined : replyToId;
104+
// Slack uses the known root for inherited replies, but explicit targets win.
105+
const resolved =
106+
replyToIsExplicit === false
107+
? (threadId ?? allowedReply)
108+
: (allowedReply ?? threadId);
109+
return {
110+
replyToId: resolved == null ? undefined : String(resolved),
111+
threadId: null,
112+
};
113+
},
110114
},
111115
},
112116
source: "test",

‎src/auto-reply/reply/followup-delivery.channel.test.ts‎

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -168,10 +168,17 @@ describe("follow-up delivery channel boundary", () => {
168168
it("dedupes later Slack replies against their actual first-mode transport thread", () => {
169169
const slack = createChannelPlugin("slack");
170170
slack.threading = {
171-
resolveReplyTransport: ({ threadId, replyToId, replyToIsExplicit }) => ({
172-
threadId: null,
173-
replyToId: replyToIsExplicit ? replyToId : threadId == null ? undefined : String(threadId),
174-
}),
171+
resolveReplyTransport: ({ threadId, replyToId, replyToIsExplicit }) => {
172+
const inheritedThread = threadId == null ? undefined : String(threadId);
173+
// First-mode can clear replyToId; Slack still falls back to the inherited thread.
174+
return {
175+
threadId: null,
176+
replyToId:
177+
replyToIsExplicit === false
178+
? (inheritedThread ?? replyToId)
179+
: (replyToId ?? inheritedThread),
180+
};
181+
},
175182
};
176183
setActivePluginRegistry(
177184
createTestRegistry([{ pluginId: "slack", plugin: slack, source: "test" }]),

‎src/auto-reply/reply/reply-payloads-dedupe.ts‎

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -200,14 +200,12 @@ function resolveOriginThreadIdForPayload(params: {
200200
replyDelivery?: ReplyDeliveryContext;
201201
}): string | undefined {
202202
const originThreadId = normalizeThreadIdForComparison(params.originatingThreadId);
203-
if (originThreadId && !params.replyToIsExplicit) {
204-
return originThreadId;
205-
}
206203
const replyToId = normalizeThreadIdForComparison(params.replyToId);
207204
const resolveReplyTransport = getChannelPlugin(params.provider)?.threading?.resolveReplyTransport;
208-
if (!replyToId || !params.config || !resolveReplyTransport) {
205+
if (!params.config || !resolveReplyTransport) {
209206
return originThreadId;
210207
}
208+
// Implicit replies can leave the inbound thread; dedupe must use the same transport as delivery.
211209
const transport = resolveReplyTransport({
212210
cfg: params.config,
213211
accountId: params.accountId,
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
import { beforeEach, describe, expect, it } from "vitest";
2+
import type { ChannelThreadingAdapter } from "../../channels/plugins/types.public.js";
3+
import { resetPluginRuntimeStateForTest, setActivePluginRegistry } from "../../plugins/runtime.js";
4+
import {
5+
createChannelTestPluginBase,
6+
createTestRegistry,
7+
} from "../../test-utils/channel-plugins.js";
8+
import { buildReplyPayloads } from "./agent-runner-payloads.js";
9+
import { resolveFollowupDeliveryPayloads } from "./followup-delivery-payloads.js";
10+
11+
describe("reply dedupe uses the plugin's delivery destination", () => {
12+
beforeEach(() => {
13+
resetPluginRuntimeStateForTest();
14+
setActivePluginRegistry(
15+
createTestRegistry([
16+
{
17+
pluginId: "test-flat",
18+
source: "test",
19+
plugin: {
20+
...createChannelTestPluginBase({ id: "test-flat" }),
21+
threading: {
22+
resolveReplyTransport: ({ replyDelivery }) =>
23+
replyDelivery?.replyToMode === "off" ? { threadId: null, replyToId: null } : null,
24+
} satisfies ChannelThreadingAdapter,
25+
},
26+
},
27+
]),
28+
);
29+
});
30+
31+
it.each([
32+
{ mode: "off", toolThread: undefined, count: 0 },
33+
{ mode: "off", toolThread: "inbound-thread", count: 1 },
34+
{ mode: "all", toolThread: undefined, count: 1 },
35+
{ mode: "all", toolThread: "inbound-thread", count: 0 },
36+
] as const)(
37+
"mode=$mode toolThread=$toolThread across immediate and queued replies",
38+
async ({ mode, toolThread, count }) => {
39+
const payloads = [{ text: "The completed answer." }];
40+
const targets = [
41+
{
42+
tool: "message",
43+
provider: "test-flat",
44+
to: "room",
45+
threadId: toolThread,
46+
text: "The completed answer.",
47+
},
48+
];
49+
const result = await buildReplyPayloads({
50+
config: {},
51+
payloads,
52+
isHeartbeat: false,
53+
didLogHeartbeatStrip: false,
54+
blockStreamingEnabled: false,
55+
blockReplyPipeline: null,
56+
replyToMode: mode,
57+
replyToChannel: "test-flat",
58+
currentMessageId: "current-message",
59+
messageProvider: "test-flat",
60+
originatingTo: "room",
61+
originatingThreadId: "inbound-thread",
62+
messagingToolSentTargets: targets,
63+
});
64+
expect(result.replyPayloads).toHaveLength(count);
65+
expect(
66+
resolveFollowupDeliveryPayloads({
67+
cfg: {},
68+
payloads,
69+
messageProvider: "test-flat",
70+
originatingTo: "room",
71+
originatingThreadId: "inbound-thread",
72+
originatingReplyToMode: mode,
73+
sentTargets: targets,
74+
}),
75+
).toHaveLength(count);
76+
},
77+
);
78+
});

0 commit comments

Comments
 (0)