From 7a09255361b7fb85f0e7a917c53660c8ac7edf71 Mon Sep 17 00:00:00 2001 From: Vincent Koc Date: Mon, 16 Mar 2026 01:01:27 -0700 Subject: [PATCH] Runtime: lazy-load channel runtime singletons --- src/auto-reply/reply/commands-session.ts | 8 +++++++- src/gateway/session-reset-service.ts | 8 +++++++- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/src/auto-reply/reply/commands-session.ts b/src/auto-reply/reply/commands-session.ts index 07f984eb92e..0359c77331b 100644 --- a/src/auto-reply/reply/commands-session.ts +++ b/src/auto-reply/reply/commands-session.ts @@ -22,7 +22,12 @@ const SESSION_COMMAND_PREFIX = "/session"; const SESSION_DURATION_OFF_VALUES = new Set(["off", "disable", "disabled", "none", "0"]); const SESSION_ACTION_IDLE = "idle"; const SESSION_ACTION_MAX_AGE = "max-age"; -const channelRuntime = createPluginRuntime().channel; +let cachedChannelRuntime: ReturnType["channel"] | undefined; + +function getChannelRuntime() { + cachedChannelRuntime ??= createPluginRuntime().channel; + return cachedChannelRuntime; +} function resolveSessionCommandUsage() { return "Usage: /session idle | /session max-age (example: /session idle 24h)"; @@ -373,6 +378,7 @@ export const handleSessionCommand: CommandHandler = async (params, allowTextComm const threadId = params.ctx.MessageThreadId != null ? String(params.ctx.MessageThreadId).trim() : ""; const telegramConversationId = onTelegram ? resolveTelegramConversationId(params) : undefined; + const channelRuntime = getChannelRuntime(); const discordManager = onDiscord ? channelRuntime.discord.threadBindings.getManager(accountId) diff --git a/src/gateway/session-reset-service.ts b/src/gateway/session-reset-service.ts index d662d60841b..48066b6e2a7 100644 --- a/src/gateway/session-reset-service.ts +++ b/src/gateway/session-reset-service.ts @@ -31,7 +31,12 @@ import { } from "./session-utils.js"; const ACP_RUNTIME_CLEANUP_TIMEOUT_MS = 15_000; -const channelRuntime = createPluginRuntime().channel; +let cachedChannelRuntime: ReturnType["channel"] | undefined; + +function getChannelRuntime() { + cachedChannelRuntime ??= createPluginRuntime().channel; + return cachedChannelRuntime; +} function stripRuntimeModelState(entry?: SessionEntry): SessionEntry | undefined { if (!entry) { @@ -71,6 +76,7 @@ export async function emitSessionUnboundLifecycleEvent(params: { emitHooks?: boolean; }) { const targetKind = isSubagentSessionKey(params.targetSessionKey) ? "subagent" : "acp"; + const channelRuntime = getChannelRuntime(); channelRuntime.discord.threadBindings.unbindBySessionKey({ targetSessionKey: params.targetSessionKey, targetKind,