Skip to content

Commit 26e76f9

Browse files
authored
fix: dedupe inbound Telegram DM replies per agent (#40519)
Merged via squash. Prepared head SHA: 6e235e7 Co-authored-by: obviyus <[email protected]> Co-authored-by: obviyus <[email protected]> Reviewed-by: @obviyus
1 parent 8befd88 commit 26e76f9

File tree

4 files changed

+79
-4
lines changed

4 files changed

+79
-4
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ Docs: https://docs.openclaw.ai
5353
- Docs/Changelog: correct the contributor credit for the bundled Control UI global-install fix to @LarytheLord. (#40420) Thanks @velvet-shark.
5454
- Models/openai-codex GPT-5.4 forward-compat: use the GPT-5.4 1,050,000-token context window and 128,000 max tokens for `openai-codex/gpt-5.4` instead of inheriting stale legacy Codex limits in resolver fallbacks and model listing. (#37876) thanks @yuweuii.
5555
- Telegram/media downloads: time out only stalled body reads so polling recovers from hung file downloads without aborting slow downloads that are still streaming data. (#40098) thanks @tysoncung.
56+
- Telegram/DM routing: dedupe inbound Telegram DMs per agent instead of per session key so the same DM cannot trigger duplicate replies when both `agent:main:main` and `agent:main:telegram:direct:<id>` resolve for one agent. Fixes #40005. Supersedes #40116. (#40519) thanks @obviyus.
5657

5758
## 2026.3.7
5859

src/auto-reply/inbound.test.ts

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,7 @@ describe("inbound dedupe", () => {
236236
).toBe(false);
237237
});
238238

239-
it("does not dedupe across session keys", () => {
239+
it("does not dedupe across agent ids", () => {
240240
resetInboundDedupe();
241241
const base: MsgContext = {
242242
Provider: "whatsapp",
@@ -248,12 +248,36 @@ describe("inbound dedupe", () => {
248248
shouldSkipDuplicateInbound({ ...base, SessionKey: "agent:alpha:main" }, { now: 100 }),
249249
).toBe(false);
250250
expect(
251-
shouldSkipDuplicateInbound({ ...base, SessionKey: "agent:bravo:main" }, { now: 200 }),
251+
shouldSkipDuplicateInbound(
252+
{ ...base, SessionKey: "agent:bravo:whatsapp:direct:+1555" },
253+
{
254+
now: 200,
255+
},
256+
),
252257
).toBe(false);
253258
expect(
254259
shouldSkipDuplicateInbound({ ...base, SessionKey: "agent:alpha:main" }, { now: 300 }),
255260
).toBe(true);
256261
});
262+
263+
it("dedupes when the same agent sees the same inbound message under different session keys", () => {
264+
resetInboundDedupe();
265+
const base: MsgContext = {
266+
Provider: "telegram",
267+
OriginatingChannel: "telegram",
268+
OriginatingTo: "telegram:7463849194",
269+
MessageSid: "msg-1",
270+
};
271+
expect(
272+
shouldSkipDuplicateInbound({ ...base, SessionKey: "agent:main:main" }, { now: 100 }),
273+
).toBe(false);
274+
expect(
275+
shouldSkipDuplicateInbound(
276+
{ ...base, SessionKey: "agent:main:telegram:direct:7463849194" },
277+
{ now: 200 },
278+
),
279+
).toBe(true);
280+
});
257281
});
258282

259283
describe("createInboundDebouncer", () => {

src/auto-reply/reply/dispatch-from-config.test.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1539,6 +1539,38 @@ describe("dispatchReplyFromConfig", () => {
15391539
expect(replyResolver).toHaveBeenCalledTimes(1);
15401540
});
15411541

1542+
it("deduplicates same-agent inbound replies across main and direct session keys", async () => {
1543+
setNoAbort();
1544+
const cfg = emptyConfig;
1545+
const replyResolver = vi.fn(async () => ({ text: "hi" }) as ReplyPayload);
1546+
const baseCtx = buildTestCtx({
1547+
Provider: "telegram",
1548+
Surface: "telegram",
1549+
OriginatingChannel: "telegram",
1550+
OriginatingTo: "telegram:7463849194",
1551+
MessageSid: "msg-1",
1552+
SessionKey: "agent:main:main",
1553+
});
1554+
1555+
await dispatchReplyFromConfig({
1556+
ctx: baseCtx,
1557+
cfg,
1558+
dispatcher: createDispatcher(),
1559+
replyResolver,
1560+
});
1561+
await dispatchReplyFromConfig({
1562+
ctx: {
1563+
...baseCtx,
1564+
SessionKey: "agent:main:telegram:direct:7463849194",
1565+
},
1566+
cfg,
1567+
dispatcher: createDispatcher(),
1568+
replyResolver,
1569+
});
1570+
1571+
expect(replyResolver).toHaveBeenCalledTimes(1);
1572+
});
1573+
15421574
it("emits message_received hook with originating channel metadata", async () => {
15431575
setNoAbort();
15441576
hookMocks.runner.hasHooks.mockReturnValue(true);

src/auto-reply/reply/inbound-dedupe.ts

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { logVerbose, shouldLogVerbose } from "../../globals.js";
22
import { createDedupeCache, type DedupeCache } from "../../infra/dedupe.js";
3+
import { parseAgentSessionKey } from "../../sessions/session-key-utils.js";
34
import type { MsgContext } from "../templating.js";
45

56
const DEFAULT_INBOUND_DEDUPE_TTL_MS = 20 * 60_000;
@@ -15,6 +16,23 @@ const normalizeProvider = (value?: string | null) => value?.trim().toLowerCase()
1516
const resolveInboundPeerId = (ctx: MsgContext) =>
1617
ctx.OriginatingTo ?? ctx.To ?? ctx.From ?? ctx.SessionKey;
1718

19+
function resolveInboundDedupeSessionScope(ctx: MsgContext): string {
20+
const sessionKey =
21+
(ctx.CommandSource === "native" ? ctx.CommandTargetSessionKey : undefined)?.trim() ||
22+
ctx.SessionKey?.trim() ||
23+
"";
24+
if (!sessionKey) {
25+
return "";
26+
}
27+
const parsed = parseAgentSessionKey(sessionKey);
28+
if (!parsed) {
29+
return sessionKey;
30+
}
31+
// The same physical inbound message should never run twice for the same
32+
// agent, even if a routing bug presents it under both main and direct keys.
33+
return `agent:${parsed.agentId}`;
34+
}
35+
1836
export function buildInboundDedupeKey(ctx: MsgContext): string | null {
1937
const provider = normalizeProvider(ctx.OriginatingChannel ?? ctx.Provider ?? ctx.Surface);
2038
const messageId = ctx.MessageSid?.trim();
@@ -25,13 +43,13 @@ export function buildInboundDedupeKey(ctx: MsgContext): string | null {
2543
if (!peerId) {
2644
return null;
2745
}
28-
const sessionKey = ctx.SessionKey?.trim() ?? "";
46+
const sessionScope = resolveInboundDedupeSessionScope(ctx);
2947
const accountId = ctx.AccountId?.trim() ?? "";
3048
const threadId =
3149
ctx.MessageThreadId !== undefined && ctx.MessageThreadId !== null
3250
? String(ctx.MessageThreadId)
3351
: "";
34-
return [provider, accountId, sessionKey, peerId, threadId, messageId].filter(Boolean).join("|");
52+
return [provider, accountId, sessionScope, peerId, threadId, messageId].filter(Boolean).join("|");
3553
}
3654

3755
export function shouldSkipDuplicateInbound(

0 commit comments

Comments
 (0)