Skip to content

Commit d0ce3f8

Browse files
committed
chore: merge latest main into gateway dotenv fix
* origin/main: chore(tui): add Gateway boundary PTY coverage (#119191) fix(telegram): route reactions to their originating forum topics test(gateway): isolate scheduled service timers (#119218)
2 parents 8ba7c0a + 52954a1 commit d0ce3f8

18 files changed

Lines changed: 877 additions & 90 deletions

‎docs/channels/telegram.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -718,7 +718,7 @@ curl "https://api.telegram.org/bot<bot_token>/getUpdates"
718718

719719
`own` means user reactions to bot-sent messages only (best-effort via a sent-message cache). Reaction events still respect Telegram access controls (`dmPolicy`, `allowFrom`, `groupPolicy`, `groupAllowFrom`); unauthorized senders are dropped.
720720

721-
Telegram does not provide thread IDs in reaction updates: non-forum groups route to the group chat session; forum groups route to the general-topic session (`:topic:1`), not the exact originating topic.
721+
Telegram does not provide thread IDs in reaction updates. Non-forum groups route to the group chat session. Forum groups recover the originating topic from OpenClaw's bounded message cache (keyed by account, chat, and message ID), so the reaction routes to that topic's session, including its topic agent and conversation bindings. When the reacted-to message is no longer cached the topic is unknown, so OpenClaw skips the reaction notification and logs a warning instead of attributing it to General (`:topic:1`).
722722

723723
`allowed_updates` for polling/webhook include `message_reaction` automatically.
724724

‎extensions/telegram/src/bot-core.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ import {
6464
import { registerTelegramOutboundGroupHistoryRecorder } from "./outbound-message-context.js";
6565
import { formatTelegramRawUpdateForLog } from "./raw-update-log.js";
6666
import { createTelegramSendChatActionHandler } from "./sendchataction-401-backoff.js";
67-
import { getTelegramSequentialKey } from "./sequential-key.js";
67+
import { getTelegramSequentialConstraints } from "./sequential-key.js";
6868
import { createTelegramThreadBindingManager } from "./thread-bindings.js";
6969

7070
type TelegramBotRuntime = {
@@ -233,7 +233,7 @@ export function createTelegramBotCore(
233233
await next();
234234
});
235235

236-
bot.use(botRuntime.sequentialize(getTelegramSequentialKey));
236+
bot.use(botRuntime.sequentialize(getTelegramSequentialConstraints));
237237

238238
const rawUpdateLogger = createSubsystemLogger("gateway/channels/telegram/raw-update");
239239

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
// Telegram tests cover forum topic recovery from the real message cache.
2+
import type { Message } from "grammy/types";
3+
import type { OpenClawConfig } from "openclaw/plugin-sdk/config-contracts";
4+
import { beforeEach, describe, expect, it } from "vitest";
5+
import { createTelegramMessageContextRuntime } from "./bot-handlers.message-context.runtime.js";
6+
import type { RegisterTelegramHandlerParams } from "./bot-native-commands.js";
7+
import { resetTelegramMessageCacheForTest } from "./runtime.test-support.js";
8+
9+
const CHAT_ID = 5678;
10+
const TOPIC_ID = 77;
11+
12+
let storeScopeId = 0;
13+
14+
/**
15+
* Builds the runtime against the real message cache so the reaction path's topic
16+
* recovery is proven through the cache it actually reads, not a stub.
17+
*/
18+
function createRuntime() {
19+
storeScopeId += 1;
20+
const cfg: OpenClawConfig = {};
21+
return createTelegramMessageContextRuntime({
22+
cfg,
23+
accountId: "default",
24+
opts: { token: "test" },
25+
telegramCfg: {},
26+
telegramDeps: {
27+
resolveStorePath: () => `/tmp/openclaw-telegram-thread-recovery-${storeScopeId}/store.json`,
28+
} as RegisterTelegramHandlerParams["telegramDeps"],
29+
});
30+
}
31+
32+
function forumMessage(messageId: number, threadId?: number): Message {
33+
return {
34+
chat: { id: CHAT_ID, type: "supergroup", title: "Forum", is_forum: true },
35+
message_id: messageId,
36+
date: 1736380800,
37+
text: "topic message",
38+
from: { id: 10, is_bot: false, first_name: "Bob" },
39+
...(threadId === undefined ? {} : { message_thread_id: threadId }),
40+
} as Message;
41+
}
42+
43+
describe("resolveCachedMessageThreadId", () => {
44+
beforeEach(() => {
45+
resetTelegramMessageCacheForTest();
46+
});
47+
48+
it("recovers the topic of a recorded forum message", async () => {
49+
const runtime = createRuntime();
50+
await runtime.recordMessageForReplyChain(forumMessage(100, TOPIC_ID), TOPIC_ID);
51+
52+
await expect(
53+
runtime.resolveCachedMessageThreadId({ chatId: CHAT_ID, messageId: 100 }),
54+
).resolves.toBe(TOPIC_ID);
55+
});
56+
57+
it("returns undefined for a message that is not in the cache", async () => {
58+
const runtime = createRuntime();
59+
60+
// Cache miss must stay unknown; the reaction handler drops rather than
61+
// attributing the reaction to the General topic.
62+
await expect(
63+
runtime.resolveCachedMessageThreadId({ chatId: CHAT_ID, messageId: 404 }),
64+
).resolves.toBeUndefined();
65+
});
66+
67+
it("returns undefined for a recorded message that carries no topic", async () => {
68+
const runtime = createRuntime();
69+
await runtime.recordMessageForReplyChain(forumMessage(101));
70+
71+
await expect(
72+
runtime.resolveCachedMessageThreadId({ chatId: CHAT_ID, messageId: 101 }),
73+
).resolves.toBeUndefined();
74+
});
75+
});

‎extensions/telegram/src/bot-handlers.message-context.runtime.ts‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import {
2323
buildTelegramReplyChain,
2424
createTelegramMessageCache,
2525
isTelegramMessageFromCurrentBot,
26+
resolveProviderObservedTelegramThreadId,
2627
type TelegramCachedMessageNode,
2728
type TelegramReplyChainEntry,
2829
} from "./message-cache.js";
@@ -92,6 +93,22 @@ export function createTelegramMessageContextRuntime({
9293
...(threadId != null ? { threadId } : {}),
9394
});
9495

96+
// `MessageReactionUpdated` carries no `message_thread_id`, so the reaction handler
97+
// recovers the originating topic from the same bounded cache that records inbound
98+
// and outbound messages. `undefined` means "thread unknown", never "General": the
99+
// caller must not substitute a topic id.
100+
const resolveCachedMessageThreadId = async (params: {
101+
chatId: number | string;
102+
messageId: number | string;
103+
}): Promise<number | undefined> => {
104+
const node = await messageCache.get({
105+
accountId,
106+
chatId: params.chatId,
107+
messageId: String(params.messageId),
108+
});
109+
return resolveProviderObservedTelegramThreadId(node);
110+
};
111+
95112
const buildReplyChainForMessage = (msg: Message) =>
96113
buildTelegramReplyChain({ cache: messageCache, accountId, chatId: msg.chat.id, msg });
97114

@@ -255,6 +272,7 @@ export function createTelegramMessageContextRuntime({
255272

256273
return {
257274
recordMessageForReplyChain,
275+
resolveCachedMessageThreadId,
258276
buildReplyChainForMessage,
259277
toReplyChainEntry,
260278
buildPromptContextForMessage,

‎extensions/telegram/src/bot-handlers.message.runtime.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ export function createTelegramHandlerMessageRuntime({
7070
const { resolveTelegramSessionState, resolvePromptContextAmbientWatermark } = sessionRuntime;
7171
const {
7272
recordMessageForReplyChain,
73+
resolveCachedMessageThreadId,
7374
buildReplyChainForMessage,
7475
toReplyChainEntry,
7576
buildPromptContextForMessage,
@@ -424,6 +425,7 @@ export function createTelegramHandlerMessageRuntime({
424425
resolveTelegramSessionState,
425426
resolvePromptContextAmbientWatermark,
426427
recordMessageForReplyChain,
428+
resolveCachedMessageThreadId,
427429
processMessageWithReplyChain,
428430
};
429431
}

0 commit comments

Comments
 (0)