@@ -98,6 +98,8 @@ export async function prepareGatewayLifecycle(params: {
9898 sessionEventSubscribers,
9999 watchNodeRequestHandler,
100100 defaultWorkspaceDir,
101+ activeTaskCount,
102+ residentRegistry,
101103 } = runtime ;
102104 const completeControlUiDeviceAuthMigrationForEffectiveOperator = (
103105 device : EffectiveOperatorDeviceIdentity ,
@@ -264,6 +266,66 @@ export async function prepareGatewayLifecycle(params: {
264266 publishMethodSurface : ( methods : readonly string [ ] ) => {
265267 runtimeState . gatewayMethods . splice ( 0 , runtimeState . gatewayMethods . length , ...methods ) ;
266268 } ,
269+ setEarlyRuntimeHandles : ( handles : {
270+ bonjourStop : typeof runtimeState . bonjourStop ;
271+ getActiveTaskCount : ( ) => number ;
272+ skillsChangeUnsub : typeof runtimeState . skillsChangeUnsub ;
273+ } ) => {
274+ runtimeState . bonjourStop = handles . bonjourStop ;
275+ activeTaskCount . get = handles . getActiveTaskCount ;
276+ runtimeState . skillsChangeUnsub = handles . skillsChangeUnsub ;
277+ } ,
278+ swapBonjourStop : ( next : typeof runtimeState . bonjourStop ) => {
279+ const previous = runtimeState . bonjourStop ;
280+ runtimeState . bonjourStop = next ;
281+ return previous ;
282+ } ,
283+ setScheduledServiceHandles : ( handles : {
284+ heartbeatRunner : typeof runtimeState . heartbeatRunner ;
285+ stopOutboundDeliveryRecovery : typeof runtimeState . stopOutboundDeliveryRecovery ;
286+ } ) => {
287+ runtimeState . heartbeatRunner = handles . heartbeatRunner ;
288+ runtimeState . stopOutboundDeliveryRecovery = handles . stopOutboundDeliveryRecovery ;
289+ } ,
290+ setPostAttachHandles : ( handles : {
291+ stopGatewayUpdateCheck : typeof runtimeState . stopGatewayUpdateCheck ;
292+ tailscaleCleanup : typeof runtimeState . tailscaleCleanup ;
293+ pluginServices : typeof runtimeState . pluginServices ;
294+ } ) => {
295+ runtimeState . stopGatewayUpdateCheck = handles . stopGatewayUpdateCheck ;
296+ runtimeState . tailscaleCleanup = handles . tailscaleCleanup ;
297+ runtimeState . pluginServices = handles . pluginServices ;
298+ } ,
299+ setPluginServices : ( pluginServices : typeof runtimeState . pluginServices ) => {
300+ runtimeState . pluginServices = pluginServices ;
301+ } ,
302+ setConfigReloaderHandle : ( configReloader : typeof runtimeState . configReloader ) => {
303+ runtimeState . configReloader = configReloader ;
304+ } ,
305+ setPostReadySidecars : ( sidecars : typeof runtimeState . postReadySidecars ) => {
306+ runtimeState . postReadySidecars = sidecars ;
307+ } ,
308+ setGatewayLifetimeSidecars : ( sidecars : typeof runtimeState . gatewayLifetimeSidecars ) => {
309+ runtimeState . gatewayLifetimeSidecars = sidecars ;
310+ } ,
311+ addGatewayLifetimeSidecar : ( sidecar : ( typeof runtimeState . gatewayLifetimeSidecars ) [ number ] ) => {
312+ runtimeState . gatewayLifetimeSidecars . push ( sidecar ) ;
313+ } ,
314+ setMaintenanceHandles : ( handles : {
315+ tickInterval : typeof runtimeState . tickInterval ;
316+ healthInterval : typeof runtimeState . healthInterval ;
317+ dedupeCleanup : typeof runtimeState . dedupeCleanup ;
318+ stopMediaCleanup : typeof runtimeState . stopMediaCleanup ;
319+ worktreeCleanup : typeof runtimeState . worktreeCleanup ;
320+ skillCuratorCleanup : typeof runtimeState . skillCuratorCleanup ;
321+ } ) => {
322+ runtimeState . tickInterval = handles . tickInterval ;
323+ runtimeState . healthInterval = handles . healthInterval ;
324+ runtimeState . dedupeCleanup = handles . dedupeCleanup ;
325+ runtimeState . stopMediaCleanup = handles . stopMediaCleanup ;
326+ runtimeState . worktreeCleanup = handles . worktreeCleanup ;
327+ runtimeState . skillCuratorCleanup = handles . skillCuratorCleanup ;
328+ } ,
267329 } ;
268330 runtimeState . controlUiSessionPullRequests = createControlUiSessionPullRequestSubscriptions ( {
269331 broadcastToConnIds,
@@ -500,28 +562,35 @@ export async function prepareGatewayLifecycle(params: {
500562 }
501563 } ;
502564
565+ const diagnosticHeartbeatResident = residentRegistry . register ( {
566+ name : "diagnostic-heartbeat" ,
567+ start : ( ) => {
568+ // Gateway lifecycle owns both this existing heartbeat timer and the monitor
569+ // it samples, so startup failure and normal close tear them down together.
570+ startDiagnosticHeartbeat ( undefined , {
571+ getConfig : getRuntimeConfig ,
572+ startupGraceMs : 60_000 ,
573+ sampleLiveness : ( ) => {
574+ const sample = readinessEventLoopHealth . persistentDegradationSnapshot ( ) ;
575+ if ( ! sample || sample . degradedSinceMs == null ) {
576+ return null ;
577+ }
578+ return {
579+ reasons : sample . reasons ,
580+ intervalMs : sample . intervalMs ,
581+ degradedSinceMs : sample . degradedSinceMs ,
582+ eventLoopDelayP99Ms : sample . delayP99Ms ,
583+ eventLoopDelayMaxMs : sample . delayMaxMs ,
584+ eventLoopUtilization : sample . utilization ,
585+ cpuCoreRatio : sample . cpuCoreRatio ,
586+ } ;
587+ } ,
588+ } ) ;
589+ } ,
590+ stop : ( ) => stopDiagnosticHeartbeat ( ) ,
591+ } ) ;
503592 if ( diagnosticsEnabled ) {
504- // Gateway lifecycle owns both this existing heartbeat timer and the monitor
505- // it samples, so startup failure and normal close tear them down together.
506- startDiagnosticHeartbeat ( undefined , {
507- getConfig : getRuntimeConfig ,
508- startupGraceMs : 60_000 ,
509- sampleLiveness : ( ) => {
510- const sample = readinessEventLoopHealth . persistentDegradationSnapshot ( ) ;
511- if ( ! sample || sample . degradedSinceMs == null ) {
512- return null ;
513- }
514- return {
515- reasons : sample . reasons ,
516- intervalMs : sample . intervalMs ,
517- degradedSinceMs : sample . degradedSinceMs ,
518- eventLoopDelayP99Ms : sample . delayP99Ms ,
519- eventLoopDelayMaxMs : sample . delayMaxMs ,
520- eventLoopUtilization : sample . utilization ,
521- cpuCoreRatio : sample . cpuCoreRatio ,
522- } ;
523- } ,
524- } ) ;
593+ diagnosticHeartbeatResident . start ( ) ;
525594 }
526595
527596 return {
0 commit comments