@@ -26,6 +26,7 @@ import { formatModelsAvailableHeader } from "openclaw/plugin-sdk/models-provider
2626import { resolveAgentRoute } from "openclaw/plugin-sdk/routing" ;
2727import { resolveThreadSessionKeys } from "openclaw/plugin-sdk/routing" ;
2828import { danger , logVerbose , warn } from "openclaw/plugin-sdk/runtime-env" ;
29+ import { resolveEffectiveQueueMode } from "openclaw/plugin-sdk/core" ;
2930import { resolveTelegramMediaRuntimeOptions } from "./accounts.js" ;
3031import { withTelegramApiErrorLogging } from "./api-logging.js" ;
3132import {
@@ -97,6 +98,7 @@ import {
9798 type ProviderInfo ,
9899} from "./model-buttons.js" ;
99100import { buildInlineKeyboard } from "./send.js" ;
101+ import { buildChatSessionCacheKey , CHAT_SESSION_CACHE_MAX } from "./sequential-key.js" ;
100102
101103export const registerTelegramHandlers = ( {
102104 cfg,
@@ -115,6 +117,7 @@ export const registerTelegramHandlers = ({
115117 processMessage,
116118 logger,
117119 telegramDeps = defaultTelegramBotDeps ,
120+ chatSessionCache,
118121} : RegisterTelegramHandlerParams ) => {
119122 const mediaRuntimeOptions = resolveTelegramMediaRuntimeOptions ( {
120123 cfg,
@@ -1102,6 +1105,86 @@ export const registerTelegramHandlers = ({
11021105 debounceLane,
11031106 } )
11041107 : null ;
1108+
1109+ // Populate chat session cache and check for debouncer bypass.
1110+ // When an agent run is active and the queue mode needs immediate delivery
1111+ // (steer/interrupt), bypass the debouncer to avoid keyChains serialization.
1112+ if ( chatSessionCache && telegramDeps . isRunActiveForSessionKey ) {
1113+ const isGroup = msg . chat . type === "group" || msg . chat . type === "supergroup" ;
1114+ const isForum = msg . chat . is_forum === true ;
1115+ try {
1116+ const sessionState = resolveTelegramSessionState ( {
1117+ chatId,
1118+ isGroup,
1119+ isForum,
1120+ resolvedThreadId,
1121+ messageThreadId : msg . message_thread_id ,
1122+ senderId,
1123+ } ) ;
1124+ // Resolve the effective queue mode using the same cascade as
1125+ // resolveQueueSettings (session → channel → global → "collect").
1126+ // This is an intentional duplication for the hot path — the canonical
1127+ // resolveQueueSettings requires full session context params that are
1128+ // not yet available at sequentializer level.
1129+ // NOTE: Per-message inline queue directives (e.g. /interrupt as a one-off) are resolved
1130+ // later in the pipeline (resolveQueueSettings). This fast path only covers sessions that
1131+ // are already in steer/interrupt mode. One-off inline overrides still work correctly via
1132+ // the normal debouncer path — they just don't get the bypass optimization.
1133+ //
1134+ // Use live config (loadConfig) rather than the outer `cfg` snapshot so that
1135+ // runtime queue-mode changes take effect immediately without a restart.
1136+ const liveCfg = telegramDeps . loadConfig ( ) ;
1137+ const effectiveMode = resolveEffectiveQueueMode ( {
1138+ cfg : liveCfg ,
1139+ channel : "telegram" ,
1140+ sessionEntry : sessionState . sessionEntry ,
1141+ } ) ;
1142+ const needsImmediate =
1143+ effectiveMode === "steer" ||
1144+ effectiveMode === "steer-backlog" ||
1145+ effectiveMode === "interrupt" ;
1146+ // For groups, key by conversation only (shared session).
1147+ // For DMs, include senderId (bridge/business-chat can route different senders differently).
1148+ const cacheSenderId = isGroup ? undefined : senderId ;
1149+ const cacheKey = buildChatSessionCacheKey ( chatId , conversationThreadId , cacheSenderId ) ;
1150+ chatSessionCache . set ( cacheKey , {
1151+ sessionKey : sessionState . sessionKey ,
1152+ needsImmediateDelivery : needsImmediate ,
1153+ } ) ;
1154+ // Evict oldest entries when cache exceeds cap (FIFO via Map insertion order).
1155+ // NOTE: Under very high fan-out (>500 distinct chats), FIFO eviction could remove an
1156+ // active session's entry before its next message arrives. In that case the bypass misses
1157+ // for one message (repopulated on the next). Acceptable for the expected bot scale;
1158+ // an LRU policy would handle this better if fan-out grows significantly.
1159+ if ( chatSessionCache . size > CHAT_SESSION_CACHE_MAX ) {
1160+ const firstKey = chatSessionCache . keys ( ) . next ( ) . value ;
1161+ if ( firstKey !== undefined ) {
1162+ chatSessionCache . delete ( firstKey ) ;
1163+ }
1164+ }
1165+
1166+ // Fast path: if a run is active and mode needs immediate delivery,
1167+ // bypass the debouncer and process immediately.
1168+ if ( needsImmediate && telegramDeps . isRunActiveForSessionKey ( sessionState . sessionKey ) ) {
1169+ const replyMedia = await resolveReplyMediaForMessage ( ctx , msg ) ;
1170+ await processMessage (
1171+ ctx ,
1172+ allMedia ,
1173+ storeAllowFrom ,
1174+ { receivedAtMs : Date . now ( ) } ,
1175+ replyMedia ,
1176+ ) ;
1177+ return ;
1178+ }
1179+ } catch ( err ) {
1180+ // Session resolution failure is non-fatal — fall through to normal debouncer path.
1181+ // Log so misconfiguration or route failures are visible.
1182+ logger . debug (
1183+ `chat session cache: session resolution failed for chat=${ chatId } : ${ String ( err ) } ` ,
1184+ ) ;
1185+ }
1186+ }
1187+
11051188 await inboundDebouncer . enqueue ( {
11061189 ctx,
11071190 msg,
0 commit comments