@@ -3,23 +3,30 @@ import { mkdtempSync, rmSync } from "node:fs";
33import { tmpdir } from "node:os" ;
44import path from "node:path" ;
55import type { Message } from "grammy/types" ;
6- import type { ChannelReplayClaimHandle } from "openclaw/plugin-sdk/persistent-dedupe" ;
6+ import {
7+ createChannelReplayGuard ,
8+ type ChannelReplayClaimHandle ,
9+ } from "openclaw/plugin-sdk/persistent-dedupe" ;
710import { resetPluginStateStoreForTests } from "openclaw/plugin-sdk/plugin-state-test-runtime" ;
811import { afterEach , beforeEach , describe , expect , it } from "vitest" ;
912import {
10- buildTelegramMessageDispatchAccountReplayKey ,
1113 claimTelegramMessageDispatchReplay ,
1214 commitTelegramMessageDispatchReplay ,
1315 createTelegramMessageDispatchReplayGuard ,
1416 releaseTelegramMessageDispatchReplay ,
15- TELEGRAM_MESSAGE_DISPATCH_DEDUPE_NAMESPACE ,
1617} from "./message-dispatch-dedupe.js" ;
1718
1819type TelegramMessageDispatchReplayGuard = Parameters <
1920 typeof claimTelegramMessageDispatchReplay
2021> [ 0 ] [ "guard" ] ;
2122
2223const tempDirs : string [ ] = [ ] ;
24+ const DEFAULT_BOT_USER_ID = 99 ;
25+ const CURRENT_NAMESPACE = "global" ;
26+ const TELEGRAM_MESSAGE_DISPATCH_DEDUPE_TTL_MS = 7 * 24 * 60 * 60 * 1000 ;
27+ const TELEGRAM_MESSAGE_DISPATCH_DEDUPE_NAMESPACE_PREFIX = "telegram.message-dispatch-dedupe" ;
28+ const TELEGRAM_MESSAGE_DISPATCH_DEDUPE_STATE_PLUGIN_ID = "telegram-message-dispatch-dedupe" ;
29+ const TELEGRAM_MESSAGE_DISPATCH_DEDUPE_STATE_MAX_ENTRIES = 50_000 ;
2330let previousStateDir : string | undefined ;
2431
2532function createStateDir ( ) : string {
@@ -36,9 +43,35 @@ function message(params?: { chatId?: number; messageId?: number }): Message {
3643 } as Message ;
3744}
3845
39- function storedReplayKey ( accountId : string , msg : Message ) : string {
46+ function storedReplayKey ( accountId : string , botUserId : number , msg : Message ) : string {
47+ return JSON . stringify ( [
48+ "account" ,
49+ accountId ,
50+ "bot" ,
51+ String ( botUserId ) ,
52+ "message" ,
53+ String ( msg . chat . id ) ,
54+ msg . message_id ,
55+ ] ) ;
56+ }
57+
58+ function legacyStoredReplayKey ( accountId : string , msg : Message ) : string {
4059 const key = JSON . stringify ( [ "message" , String ( msg . chat . id ) , msg . message_id ] ) ;
41- return buildTelegramMessageDispatchAccountReplayKey ( { accountId, key } ) ;
60+ return JSON . stringify ( [ "account" , accountId , key ] ) ;
61+ }
62+
63+ function createLegacyReplayGuard ( ) {
64+ return createChannelReplayGuard < { accountId : string ; msg : Message } > ( {
65+ dedupe : {
66+ ttlMs : TELEGRAM_MESSAGE_DISPATCH_DEDUPE_TTL_MS ,
67+ memoryMaxSize : 50_000 ,
68+ pluginId : TELEGRAM_MESSAGE_DISPATCH_DEDUPE_STATE_PLUGIN_ID ,
69+ namespacePrefix : TELEGRAM_MESSAGE_DISPATCH_DEDUPE_NAMESPACE_PREFIX ,
70+ stateMaxEntries : TELEGRAM_MESSAGE_DISPATCH_DEDUPE_STATE_MAX_ENTRIES ,
71+ } ,
72+ buildReplayKey : ( event ) => legacyStoredReplayKey ( event . accountId , event . msg ) ,
73+ namespace : ( ) => CURRENT_NAMESPACE ,
74+ } ) ;
4275}
4376
4477function createTestReplayGuard (
@@ -106,13 +139,14 @@ describe("Telegram message dispatch replay guard", () => {
106139 const first = await claimTelegramMessageDispatchReplay ( {
107140 guard : writer ,
108141 accountId : "default" ,
142+ botUserId : DEFAULT_BOT_USER_ID ,
109143 msg : message ( ) ,
110144 } ) ;
111145
112146 if ( first . kind !== "claimed" ) {
113147 throw new Error ( "expected initial claim" ) ;
114148 }
115- expect ( first . handle . keys ) . toEqual ( [ storedReplayKey ( "default" , message ( ) ) ] ) ;
149+ expect ( first . handle . keys ) . toEqual ( [ storedReplayKey ( "default" , DEFAULT_BOT_USER_ID , message ( ) ) ] ) ;
116150 await commitTelegramMessageDispatchReplay ( {
117151 guard : writer ,
118152 claims : [ first . handle ] ,
@@ -123,18 +157,78 @@ describe("Telegram message dispatch replay guard", () => {
123157 claimTelegramMessageDispatchReplay ( {
124158 guard : reader ,
125159 accountId : "default" ,
160+ botUserId : DEFAULT_BOT_USER_ID ,
126161 msg : message ( ) ,
127162 } ) ,
128163 ) . resolves . toEqual ( { kind : "duplicate" } ) ;
129164 } ) ;
130165
166+ it ( "isolates identical message coordinates across bot identities" , async ( ) => {
167+ const writer = createTelegramMessageDispatchReplayGuard ( ) ;
168+ const first = await claimTelegramMessageDispatchReplay ( {
169+ guard : writer ,
170+ accountId : "default" ,
171+ botUserId : 101 ,
172+ msg : message ( ) ,
173+ } ) ;
174+ if ( first . kind !== "claimed" ) {
175+ throw new Error ( "expected first bot claim" ) ;
176+ }
177+ await first . handle . commit ( ) ;
178+
179+ const second = await claimTelegramMessageDispatchReplay ( {
180+ guard : writer ,
181+ accountId : "default" ,
182+ botUserId : 202 ,
183+ msg : message ( ) ,
184+ } ) ;
185+ expect ( second . kind ) . toBe ( "claimed" ) ;
186+ if ( second . kind === "claimed" ) {
187+ await second . handle . commit ( ) ;
188+ }
189+
190+ const reader = createTelegramMessageDispatchReplayGuard ( ) ;
191+ for ( const botUserId of [ 101 , 202 ] ) {
192+ await expect (
193+ claimTelegramMessageDispatchReplay ( {
194+ guard : reader ,
195+ accountId : "default" ,
196+ botUserId,
197+ msg : message ( ) ,
198+ } ) ,
199+ ) . resolves . toEqual ( { kind : "duplicate" } ) ;
200+ }
201+ } ) ;
202+
203+ it ( "starts a fresh dedupe window when legacy rows lack bot identity" , async ( ) => {
204+ const legacy = createLegacyReplayGuard ( ) ;
205+ const legacyClaim = await legacy . claim ( { accountId : "default" , msg : message ( ) } ) ;
206+ if ( legacyClaim . kind !== "claimed" ) {
207+ throw new Error ( "expected legacy claim" ) ;
208+ }
209+ await legacyClaim . handle . commit ( ) ;
210+
211+ const current = createTelegramMessageDispatchReplayGuard ( ) ;
212+ const currentClaim = await claimTelegramMessageDispatchReplay ( {
213+ guard : current ,
214+ accountId : "default" ,
215+ botUserId : DEFAULT_BOT_USER_ID ,
216+ msg : message ( ) ,
217+ } ) ;
218+ expect ( currentClaim . kind ) . toBe ( "claimed" ) ;
219+ if ( currentClaim . kind === "claimed" ) {
220+ currentClaim . handle . release ( ) ;
221+ }
222+ } ) ;
223+
131224 it ( "preserves concurrent commits" , async ( ) => {
132225 const writer = createTelegramMessageDispatchReplayGuard ( ) ;
133226 const claims = await Promise . all (
134227 Array . from ( { length : 400 } , async ( _ , index ) => {
135228 const claim = await claimTelegramMessageDispatchReplay ( {
136229 guard : writer ,
137230 accountId : "default" ,
231+ botUserId : DEFAULT_BOT_USER_ID ,
138232 msg : message ( { messageId : index + 1 } ) ,
139233 } ) ;
140234 if ( claim . kind !== "claimed" ) {
@@ -150,9 +244,7 @@ describe("Telegram message dispatch replay guard", () => {
150244 } ) ;
151245
152246 const reader = createTelegramMessageDispatchReplayGuard ( ) ;
153- await expect ( reader . warmup ( TELEGRAM_MESSAGE_DISPATCH_DEDUPE_NAMESPACE ) ) . resolves . toBe (
154- claims . length ,
155- ) ;
247+ await expect ( reader . warmup ( CURRENT_NAMESPACE ) ) . resolves . toBe ( claims . length ) ;
156248 } ) ;
157249
158250 it ( "commits replay keys serially before starting the next write" , async ( ) => {
@@ -292,11 +384,13 @@ describe("Telegram message dispatch replay guard", () => {
292384 const first = await claimTelegramMessageDispatchReplay ( {
293385 guard : writer ,
294386 accountId : "default" ,
387+ botUserId : DEFAULT_BOT_USER_ID ,
295388 msg : message ( ) ,
296389 } ) ;
297390 const second = await claimTelegramMessageDispatchReplay ( {
298391 guard : writer ,
299392 accountId : "work" ,
393+ botUserId : DEFAULT_BOT_USER_ID ,
300394 msg : message ( ) ,
301395 } ) ;
302396 if ( first . kind !== "claimed" || second . kind !== "claimed" ) {
@@ -309,7 +403,7 @@ describe("Telegram message dispatch replay guard", () => {
309403 } ) ;
310404
311405 const reader = createTelegramMessageDispatchReplayGuard ( ) ;
312- await expect ( reader . warmup ( TELEGRAM_MESSAGE_DISPATCH_DEDUPE_NAMESPACE ) ) . resolves . toBe ( 2 ) ;
406+ await expect ( reader . warmup ( CURRENT_NAMESPACE ) ) . resolves . toBe ( 2 ) ;
313407 await expect ( reader . warmup ( "default" ) ) . resolves . toBe ( 0 ) ;
314408 } ) ;
315409
@@ -318,6 +412,7 @@ describe("Telegram message dispatch replay guard", () => {
318412 const first = await claimTelegramMessageDispatchReplay ( {
319413 guard,
320414 accountId : "default" ,
415+ botUserId : DEFAULT_BOT_USER_ID ,
321416 msg : message ( ) ,
322417 } ) ;
323418 if ( first . kind !== "claimed" ) {
@@ -327,11 +422,12 @@ describe("Telegram message dispatch replay guard", () => {
327422 const work = await claimTelegramMessageDispatchReplay ( {
328423 guard,
329424 accountId : "work" ,
425+ botUserId : DEFAULT_BOT_USER_ID ,
330426 msg : message ( ) ,
331427 } ) ;
332428 expect ( work . kind ) . toBe ( "claimed" ) ;
333429 if ( work . kind === "claimed" ) {
334- expect ( work . handle . keys ) . toEqual ( [ storedReplayKey ( "work" , message ( ) ) ] ) ;
430+ expect ( work . handle . keys ) . toEqual ( [ storedReplayKey ( "work" , DEFAULT_BOT_USER_ID , message ( ) ) ] ) ;
335431 }
336432
337433 releaseTelegramMessageDispatchReplay ( {
@@ -340,6 +436,7 @@ describe("Telegram message dispatch replay guard", () => {
340436 const retry = await claimTelegramMessageDispatchReplay ( {
341437 guard,
342438 accountId : "default" ,
439+ botUserId : DEFAULT_BOT_USER_ID ,
343440 msg : message ( ) ,
344441 } ) ;
345442 expect ( retry . kind ) . toBe ( "claimed" ) ;
@@ -353,6 +450,7 @@ describe("Telegram message dispatch replay guard", () => {
353450 const first = await claimTelegramMessageDispatchReplay ( {
354451 guard,
355452 accountId : "default" ,
453+ botUserId : DEFAULT_BOT_USER_ID ,
356454 msg : message ( ) ,
357455 } ) ;
358456 if ( first . kind !== "claimed" ) {
@@ -362,6 +460,7 @@ describe("Telegram message dispatch replay guard", () => {
362460 const duplicate = claimTelegramMessageDispatchReplay ( {
363461 guard,
364462 accountId : "default" ,
463+ botUserId : DEFAULT_BOT_USER_ID ,
365464 msg : message ( ) ,
366465 } ) ;
367466 releaseTelegramMessageDispatchReplay ( {
0 commit comments