@@ -7,7 +7,12 @@ import { formatErrorMessage } from "../infra/errors.js";
77import { resetDirectoryCache } from "../infra/outbound/target-resolver.js" ;
88import type { createSubsystemLogger } from "../logging/subsystem.js" ;
99import type { PluginRuntime } from "../plugins/runtime/types.js" ;
10- import { DEFAULT_ACCOUNT_ID } from "../routing/session-key.js" ;
10+ import { resolveAccountEntry } from "../routing/account-lookup.js" ;
11+ import {
12+ DEFAULT_ACCOUNT_ID ,
13+ normalizeAccountId ,
14+ normalizeOptionalAccountId ,
15+ } from "../routing/session-key.js" ;
1116import type { RuntimeEnv } from "../runtime.js" ;
1217
1318const CHANNEL_RESTART_POLICY : BackoffPolicy = {
@@ -31,6 +36,16 @@ type ChannelRuntimeStore = {
3136 runtimes : Map < string , ChannelAccountSnapshot > ;
3237} ;
3338
39+ type HealthMonitorConfig = {
40+ healthMonitor ?: {
41+ enabled ?: boolean ;
42+ } ;
43+ } ;
44+
45+ type ChannelHealthMonitorConfig = HealthMonitorConfig & {
46+ accounts ?: Record < string , HealthMonitorConfig > ;
47+ } ;
48+
3449function createRuntimeStore ( ) : ChannelRuntimeStore {
3550 return {
3651 aborts : new Map ( ) ,
@@ -120,45 +135,60 @@ export function createChannelManager(opts: ChannelManagerOptions): ChannelManage
120135
121136 const restartKey = ( channelId : ChannelId , accountId : string ) => `${ channelId } :${ accountId } ` ;
122137
138+ const resolveAccountHealthMonitorOverride = (
139+ channelConfig : ChannelHealthMonitorConfig | undefined ,
140+ accountId : string ,
141+ ) : boolean | undefined => {
142+ if ( ! channelConfig ?. accounts ) {
143+ return undefined ;
144+ }
145+ const direct = resolveAccountEntry ( channelConfig . accounts , accountId ) ;
146+ if ( typeof direct ?. healthMonitor ?. enabled === "boolean" ) {
147+ return direct . healthMonitor . enabled ;
148+ }
149+
150+ const normalizedAccountId = normalizeOptionalAccountId ( accountId ) ;
151+ if ( ! normalizedAccountId ) {
152+ return undefined ;
153+ }
154+ const matchKey = Object . keys ( channelConfig . accounts ) . find (
155+ ( key ) => normalizeAccountId ( key ) === normalizedAccountId ,
156+ ) ;
157+ if ( ! matchKey ) {
158+ return undefined ;
159+ }
160+ return channelConfig . accounts [ matchKey ] ?. healthMonitor ?. enabled ;
161+ } ;
162+
123163 const isHealthMonitorEnabled = ( channelId : ChannelId , accountId : string ) : boolean => {
124164 const cfg = loadConfig ( ) ;
125- const plugin = getChannelPlugin ( channelId ) ;
126- const resolvedAccount = plugin ?. config . resolveAccount ( cfg , accountId ) as
127- | {
128- healthMonitor ?: {
129- enabled ?: boolean ;
130- } ;
131- config ?: {
132- healthMonitor ?: {
133- enabled ?: boolean ;
134- } ;
135- } ;
136- }
137- | undefined ;
138- const accountOverride = resolvedAccount ?. healthMonitor ?. enabled ;
139- const wrappedAccountOverride = resolvedAccount ?. config ?. healthMonitor ?. enabled ;
140- const channelOverride = (
141- cfg . channels ?. [ channelId ] as
142- | {
143- healthMonitor ?: {
144- enabled ?: boolean ;
145- } ;
146- }
147- | undefined
148- ) ?. healthMonitor ?. enabled ;
165+ const channelConfig = cfg . channels ?. [ channelId ] as ChannelHealthMonitorConfig | undefined ;
166+ const accountOverride = resolveAccountHealthMonitorOverride ( channelConfig , accountId ) ;
167+ const channelOverride = channelConfig ?. healthMonitor ?. enabled ;
149168
150169 if ( typeof accountOverride === "boolean" ) {
151170 return accountOverride ;
152171 }
153172
154- if ( typeof wrappedAccountOverride === "boolean" ) {
155- return wrappedAccountOverride ;
156- }
157-
158173 if ( typeof channelOverride === "boolean" ) {
159174 return channelOverride ;
160175 }
161176
177+ const plugin = getChannelPlugin ( channelId ) ;
178+ if ( ! plugin ) {
179+ return true ;
180+ }
181+ try {
182+ // Probe only: health-monitor config is read directly from raw channel config above.
183+ // This call exists solely to fail closed if resolver-side config loading is broken.
184+ plugin . config . resolveAccount ( cfg , accountId ) ;
185+ } catch ( err ) {
186+ channelLogs [ channelId ] . warn ?.(
187+ `[${ channelId } :${ accountId } ] health-monitor: failed to resolve account; skipping monitor (${ formatErrorMessage ( err ) } )` ,
188+ ) ;
189+ return false ;
190+ }
191+
162192 return true ;
163193 } ;
164194
0 commit comments