From 4399782bea42be5f97381f7b89cc9c7060b827b1 Mon Sep 17 00:00:00 2001 From: Tak Hoffman <781889+Takhoffman@users.noreply.github.com> Date: Sat, 14 Mar 2026 20:17:45 -0500 Subject: [PATCH] gateway: reuse account resolution for health monitor overrides --- src/gateway/server-channels.test.ts | 61 +++++++++++++++++++++++++++-- src/gateway/server-channels.ts | 27 +++++-------- 2 files changed, 68 insertions(+), 20 deletions(-) diff --git a/src/gateway/server-channels.test.ts b/src/gateway/server-channels.test.ts index c442c142417..4627dc681fa 100644 --- a/src/gateway/server-channels.test.ts +++ b/src/gateway/server-channels.test.ts @@ -44,12 +44,13 @@ function createTestPlugin(params?: { account?: TestAccount; startAccount?: NonNullable["gateway"]>["startAccount"]; includeDescribeAccount?: boolean; + resolveAccount?: ChannelPlugin["config"]["resolveAccount"]; }): ChannelPlugin { const account = params?.account ?? { enabled: true, configured: true }; const includeDescribeAccount = params?.includeDescribeAccount !== false; const config: ChannelPlugin["config"] = { listAccountIds: () => [DEFAULT_ACCOUNT_ID], - resolveAccount: () => account, + resolveAccount: params?.resolveAccount ?? (() => account), isEnabled: (resolved) => resolved.enabled !== false, }; if (includeDescribeAccount) { @@ -88,13 +89,16 @@ function installTestRegistry(plugin: ChannelPlugin) { setActivePluginRegistry(registry); } -function createManager(options?: { channelRuntime?: PluginRuntime["channel"] }) { +function createManager(options?: { + channelRuntime?: PluginRuntime["channel"]; + loadConfig?: () => Record; +}) { const log = createSubsystemLogger("gateway/server-channels-test"); const channelLogs = { discord: log } as Record; const runtime = runtimeForLogger(log); const channelRuntimeEnvs = { discord: runtime } as Record; return createChannelManager({ - loadConfig: () => ({}), + loadConfig: () => options?.loadConfig?.() ?? {}, channelLogs, channelRuntimeEnvs, ...(options?.channelRuntime ? { channelRuntime: options.channelRuntime } : {}), @@ -180,4 +184,55 @@ describe("server-channels auto restart", () => { await manager.startChannels(); expect(startAccount).toHaveBeenCalledTimes(1); }); + + it("reuses plugin account resolution for health monitor overrides", () => { + installTestRegistry( + createTestPlugin({ + resolveAccount: (cfg, accountId) => { + const accounts = ( + cfg as { + channels?: { + discord?: { + accounts?: Record< + string, + TestAccount & { healthMonitor?: { enabled?: boolean } } + >; + }; + }; + } + ).channels?.discord?.accounts; + if (!accounts) { + return { enabled: true, configured: true }; + } + const direct = accounts[accountId ?? DEFAULT_ACCOUNT_ID]; + if (direct) { + return direct; + } + const normalized = (accountId ?? DEFAULT_ACCOUNT_ID).toLowerCase().replaceAll(" ", "-"); + const matchKey = Object.keys(accounts).find( + (key) => key.toLowerCase().replaceAll(" ", "-") === normalized, + ); + return matchKey ? (accounts[matchKey] ?? { enabled: true, configured: true }) : {}; + }, + }), + ); + + const manager = createManager({ + loadConfig: () => ({ + channels: { + discord: { + accounts: { + "Router D": { + enabled: true, + configured: true, + healthMonitor: { enabled: false }, + }, + }, + }, + }, + }), + }); + + expect(manager.isHealthMonitorEnabled("discord", "router-d")).toBe(false); + }); }); diff --git a/src/gateway/server-channels.ts b/src/gateway/server-channels.ts index 8e5e1164dc8..853a7ea551c 100644 --- a/src/gateway/server-channels.ts +++ b/src/gateway/server-channels.ts @@ -31,16 +31,6 @@ type ChannelRuntimeStore = { runtimes: Map; }; -type RawHealthMonitorEntry = { - healthMonitor?: { - enabled?: boolean; - }; -}; - -type RawChannelConfig = RawHealthMonitorEntry & { - accounts?: Record; -}; - function createRuntimeStore(): ChannelRuntimeStore { return { aborts: new Map(), @@ -132,17 +122,20 @@ export function createChannelManager(opts: ChannelManagerOptions): ChannelManage const isHealthMonitorEnabled = (channelId: ChannelId, accountId: string): boolean => { const cfg = loadConfig(); - const channelConfig = cfg.channels?.[channelId] as RawChannelConfig | undefined; - const accountOverride = channelConfig?.accounts?.[accountId]?.healthMonitor?.enabled; + const plugin = getChannelPlugin(channelId); + const resolvedAccount = plugin?.config.resolveAccount(cfg, accountId) as + | { + healthMonitor?: { + enabled?: boolean; + }; + } + | undefined; + const accountOverride = resolvedAccount?.healthMonitor?.enabled; + if (typeof accountOverride === "boolean") { return accountOverride; } - const channelOverride = channelConfig?.healthMonitor?.enabled; - if (typeof channelOverride === "boolean") { - return channelOverride; - } - return true; };