Skip to content

Commit 2b0ceb5

Browse files
authored
refactor(gateway): invert config reloader ownership (#122004)
1 parent f1bb7cd commit 2b0ceb5

6 files changed

Lines changed: 81 additions & 64 deletions

‎src/gateway/server-core-runtime.ts‎

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -571,17 +571,19 @@ export async function startGatewayCoreRuntime(input: {
571571
workspaceDir: defaultWorkspaceDir,
572572
});
573573
replaceAttachedPluginRuntime(loaded);
574-
runtimeState.pluginServices = null;
574+
kernel.setPluginServices(null);
575575
if (previousPluginServices) {
576576
await previousPluginServices.stop();
577577
}
578578
await refreshAttachedGatewayDiscovery(loaded.pluginRegistry);
579-
runtimeState.pluginServices = await startPluginServices({
580-
registry: loaded.pluginRegistry,
581-
config: params.nextConfig,
582-
workspaceDir: defaultWorkspaceDir,
583-
broadcastPluginEvent,
584-
});
579+
kernel.setPluginServices(
580+
await startPluginServices({
581+
registry: loaded.pluginRegistry,
582+
config: params.nextConfig,
583+
workspaceDir: defaultWorkspaceDir,
584+
broadcastPluginEvent,
585+
}),
586+
);
585587
const afterChannelTargets = listAttachedChannelConfigTargets();
586588
const afterChannelIds = new Set(afterChannelTargets.keys());
587589
const restartChannels = new Set<ChannelId>();
@@ -606,6 +608,10 @@ export async function startGatewayCoreRuntime(input: {
606608

607609
return {
608610
...runtime,
611+
kernel: {
612+
...kernel,
613+
reloadPlugins: reloadAttachedGatewayPlugins,
614+
},
609615
earlyRuntime,
610616
sessionCompanion,
611617
sessionObserver,
@@ -622,7 +628,6 @@ export async function startGatewayCoreRuntime(input: {
622628
getAttachedGatewayMethodRegistry: () => attachedGatewayMethodRegistry,
623629
replaceAttachedPluginRuntime,
624630
refreshAttachedGatewayDiscovery,
625-
reloadAttachedGatewayPlugins,
626631
loadGatewayModelCatalog,
627632
loadGatewayModelCatalogSnapshot,
628633
readPreparedGatewayModelCatalog,

‎src/gateway/server-kernel-request-runtime.ts‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ export async function prepareGatewayKernelRequestRuntime(params: {
9797
lifecycle,
9898
startupState,
9999
clearFallbackGatewayContextForServer,
100+
kernel,
100101
} = runtime;
101102
const chatMetadataLifecycle = await createGatewayChatMetadataLifecycle({
102103
getConfig: getRuntimeConfig,
@@ -200,6 +201,8 @@ export async function prepareGatewayKernelRequestRuntime(params: {
200201
broadcastVoiceWakeChanged,
201202
unavailableGatewayMethods,
202203
broadcastVoiceWakeRoutingChanged,
204+
notifyPluginMetadataChanged: kernel.notifyPluginMetadataChanged,
205+
getConfigReloaderHotReloadStatus: kernel.getConfigReloaderHotReloadStatus,
203206
});
204207
});
205208
bindApprovalPublicationContext(gatewayRequestContext);

‎src/gateway/server-lifecycle.ts‎

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,38 @@ export async function prepareGatewayLifecycle(params: {
302302
setConfigReloaderHandle: (configReloader: typeof runtimeState.configReloader) => {
303303
runtimeState.configReloader = configReloader;
304304
},
305+
getReloadState: () => ({
306+
hooksConfig: runtimeState.hooksConfig,
307+
hookClientIpConfig: runtimeState.hookClientIpConfig,
308+
heartbeatRunner: runtimeState.heartbeatRunner,
309+
cronState: runtimeState.cronState,
310+
channelHealthMonitor: runtimeState.channelHealthMonitor,
311+
}),
312+
setReloadHookState: (next: {
313+
hooksConfig: typeof runtimeState.hooksConfig;
314+
hookClientIpConfig: typeof runtimeState.hookClientIpConfig;
315+
}) => {
316+
runtimeState.hooksConfig = next.hooksConfig;
317+
runtimeState.hookClientIpConfig = next.hookClientIpConfig;
318+
},
319+
swapHeartbeatRunner: (next: typeof runtimeState.heartbeatRunner) => {
320+
const previous = runtimeState.heartbeatRunner;
321+
runtimeState.heartbeatRunner = next;
322+
return previous;
323+
},
324+
swapCronState: (next: typeof runtimeState.cronState) => {
325+
const previous = runtimeState.cronState;
326+
runtimeState.cronState = next;
327+
deps.cron = next.cron;
328+
return previous;
329+
},
330+
setChannelHealthMonitor: (next: typeof runtimeState.channelHealthMonitor) => {
331+
runtimeState.channelHealthMonitor = next;
332+
},
333+
notifyPluginMetadataChanged: () => {
334+
runtimeState.configReloader.notifyPluginMetadataChanged();
335+
},
336+
getConfigReloaderHotReloadStatus: () => runtimeState.configReloader.hotReloadStatus?.(),
305337
setPostReadySidecars: (sidecars: typeof runtimeState.postReadySidecars) => {
306338
runtimeState.postReadySidecars = sidecars;
307339
},
@@ -461,7 +493,7 @@ export async function prepareGatewayLifecycle(params: {
461493
...optsResult,
462494
getRuntimeSnapshot,
463495
getEventLoopHealth: readinessEventLoopHealth.snapshot,
464-
getConfigReloaderHotReloadStatus: () => runtimeState?.configReloader.hotReloadStatus?.(),
496+
getConfigReloaderHotReloadStatus: kernel.getConfigReloaderHotReloadStatus,
465497
});
466498
const stopRegisteredPostReadySidecars = async () => {
467499
const postReadySidecars = runtimeState.postReadySidecars;

‎src/gateway/server-request-context.test.ts‎

Lines changed: 20 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -24,16 +24,12 @@ function makeContextParams(
2424
overrides: Partial<GatewayRequestContextParams> = {},
2525
): GatewayRequestContextParams {
2626
const config = {} as never;
27-
const runtimeState: Pick<GatewayServerLiveState, "cronState" | "configReloader"> = {
27+
const runtimeState: Pick<GatewayServerLiveState, "cronState"> = {
2828
cronState: {
2929
cron: { start: vi.fn(), stop: vi.fn() } as never,
3030
storePath: "/tmp/cron",
3131
cronEnabled: true,
3232
},
33-
configReloader: {
34-
stop: vi.fn(async () => {}),
35-
notifyPluginMetadataChanged: vi.fn(),
36-
},
3733
};
3834
return {
3935
deps: {} as never,
@@ -103,6 +99,8 @@ function makeContextParams(
10399
channelWizardRunner: vi.fn(async () => undefined),
104100
broadcastVoiceWakeChanged: vi.fn(),
105101
broadcastVoiceWakeRoutingChanged: vi.fn(),
102+
notifyPluginMetadataChanged: vi.fn(),
103+
getConfigReloaderHotReloadStatus: vi.fn(() => undefined),
106104
unavailableGatewayMethods: new Set(),
107105
...overrides,
108106
};
@@ -161,16 +159,12 @@ describe("createGatewayRequestContext", () => {
161159
it("reads cron state live from runtime state", () => {
162160
const cronA = { start: vi.fn(), stop: vi.fn() } as never;
163161
const cronB = { start: vi.fn(), stop: vi.fn() } as never;
164-
const runtimeState: Pick<GatewayServerLiveState, "cronState" | "configReloader"> = {
162+
const runtimeState: Pick<GatewayServerLiveState, "cronState"> = {
165163
cronState: {
166164
cron: cronA,
167165
storePath: "/tmp/cron-a",
168166
cronEnabled: true,
169167
},
170-
configReloader: {
171-
stop: vi.fn(async () => {}),
172-
notifyPluginMetadataChanged: vi.fn(),
173-
},
174168
};
175169

176170
const context = createGatewayRequestContext(makeContextParams({ runtimeState }));
@@ -188,38 +182,30 @@ describe("createGatewayRequestContext", () => {
188182
expect(context.cronStorePath).toBe("/tmp/cron-b");
189183
});
190184

191-
it("reads config hot-reload status live from runtime state", () => {
192-
const runtimeState: Pick<GatewayServerLiveState, "cronState" | "configReloader"> = {
193-
cronState: {
194-
cron: { start: vi.fn(), stop: vi.fn() } as never,
195-
storePath: "/tmp/cron",
196-
cronEnabled: true,
197-
},
198-
configReloader: {
199-
stop: vi.fn(async () => {}),
200-
notifyPluginMetadataChanged: vi.fn(),
201-
},
202-
};
203-
204-
const context = createGatewayRequestContext(makeContextParams({ runtimeState }));
185+
it("reads config hot-reload status through the live kernel bridge", () => {
186+
let status: "active" | "disabled" | undefined;
187+
const context = createGatewayRequestContext(
188+
makeContextParams({ getConfigReloaderHotReloadStatus: () => status }),
189+
);
205190

206191
expect(context.getConfigReloaderHotReloadStatus?.()).toBeUndefined();
207192

208-
runtimeState.configReloader = {
209-
stop: vi.fn(async () => {}),
210-
hotReloadStatus: () => "active",
211-
notifyPluginMetadataChanged: vi.fn(),
212-
};
193+
status = "active";
213194
expect(context.getConfigReloaderHotReloadStatus?.()).toBe("active");
214195

215-
runtimeState.configReloader = {
216-
stop: vi.fn(async () => {}),
217-
hotReloadStatus: () => "disabled",
218-
notifyPluginMetadataChanged: vi.fn(),
219-
};
196+
status = "disabled";
220197
expect(context.getConfigReloaderHotReloadStatus?.()).toBe("disabled");
221198
});
222199

200+
it("routes plugin metadata changes through the kernel bridge", () => {
201+
const notifyPluginMetadataChanged = vi.fn();
202+
const context = createGatewayRequestContext(makeContextParams({ notifyPluginMetadataChanged }));
203+
204+
context.notifyPluginMetadataChanged();
205+
206+
expect(notifyPluginMetadataChanged).toHaveBeenCalledOnce();
207+
});
208+
223209
it("does not treat scoped CLI or backend callers as approval delivery routes", () => {
224210
const clients = new Set([
225211
makeGatewayClient({

‎src/gateway/server-request-context.ts‎

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ type GatewayRequestContextParams = {
2828
deps: GatewayRequestContext["deps"];
2929
runtimeState: Pick<
3030
GatewayServerLiveState,
31-
"cronState" | "configReloader" | "controlUiSessionPullRequests" | "sessionViewerPresence"
31+
"cronState" | "controlUiSessionPullRequests" | "sessionViewerPresence"
3232
>;
3333
getRuntimeConfig: GatewayRequestContext["getRuntimeConfig"];
3434
sessionCompanion: SessionCompanionService;
@@ -110,6 +110,8 @@ type GatewayRequestContextParams = {
110110
channelWizardRunner: GatewayRequestContext["channelWizardRunner"];
111111
broadcastVoiceWakeChanged: GatewayRequestContext["broadcastVoiceWakeChanged"];
112112
broadcastVoiceWakeRoutingChanged: GatewayRequestContext["broadcastVoiceWakeRoutingChanged"];
113+
notifyPluginMetadataChanged: GatewayRequestContext["notifyPluginMetadataChanged"];
114+
getConfigReloaderHotReloadStatus: GatewayRequestContext["getConfigReloaderHotReloadStatus"];
113115
unavailableGatewayMethods: ReadonlySet<string>;
114116
};
115117

@@ -175,8 +177,7 @@ export function createGatewayRequestContext(
175177
sessionViewerPresence: params.runtimeState.sessionViewerPresence,
176178
sessionCompanion: params.sessionCompanion,
177179
sessionObserver: params.sessionObserver,
178-
notifyPluginMetadataChanged: () =>
179-
params.runtimeState.configReloader.notifyPluginMetadataChanged(),
180+
notifyPluginMetadataChanged: params.notifyPluginMetadataChanged,
180181
getMcpAppSandboxPort: params.getMcpAppSandboxPort,
181182
ensureSandboxHostPort: params.ensureSandboxHostPort,
182183
resolveTerminalLaunchPolicy: params.resolveTerminalLaunchPolicy,
@@ -397,7 +398,7 @@ export function createGatewayRequestContext(
397398
purgeWizardSession: params.purgeWizardSession,
398399
getRuntimeSnapshot: params.getRuntimeSnapshot,
399400
getEventLoopHealth: params.getEventLoopHealth,
400-
getConfigReloaderHotReloadStatus: () => params.runtimeState.configReloader.hotReloadStatus?.(),
401+
getConfigReloaderHotReloadStatus: params.getConfigReloaderHotReloadStatus,
401402
startChannel: params.startChannel,
402403
stopChannel: params.stopChannel,
403404
markChannelLoggedOut: params.markChannelLoggedOut,

‎src/gateway/server-startup-finish.ts‎

Lines changed: 7 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,6 @@ export async function finishGatewayStartup(params: {
124124
activateRuntimeSecrets,
125125
applyFixedGatewayOverlays,
126126
resolveSharedGatewaySessionGenerationForConfig,
127-
reloadAttachedGatewayPlugins,
128127
stopRegisteredPostReadySidecars,
129128
chatMetadataLifecycle,
130129
gatewayRequestContext,
@@ -404,30 +403,21 @@ export async function finishGatewayStartup(params: {
404403
}),
405404
deps,
406405
broadcast,
407-
getState: () => ({
408-
hooksConfig: runtimeState.hooksConfig,
409-
hookClientIpConfig: runtimeState.hookClientIpConfig,
410-
heartbeatRunner: runtimeState.heartbeatRunner,
411-
cronState: runtimeState.cronState,
412-
channelHealthMonitor: runtimeState.channelHealthMonitor,
413-
}),
406+
getState: kernel.getReloadState,
414407
setState: (nextState) => {
415-
const cronStateChanged = nextState.cronState !== runtimeState.cronState;
416-
runtimeState.hooksConfig = nextState.hooksConfig;
417-
runtimeState.hookClientIpConfig = nextState.hookClientIpConfig;
418-
runtimeState.heartbeatRunner = nextState.heartbeatRunner;
419-
runtimeState.cronState = nextState.cronState;
420-
deps.cron = runtimeState.cronState.cron;
421-
runtimeState.channelHealthMonitor = nextState.channelHealthMonitor;
422-
if (cronStateChanged) {
408+
kernel.setReloadHookState(nextState);
409+
kernel.swapHeartbeatRunner(nextState.heartbeatRunner);
410+
const previousCronState = kernel.swapCronState(nextState.cronState);
411+
kernel.setChannelHealthMonitor(nextState.channelHealthMonitor);
412+
if (previousCronState !== nextState.cronState) {
423413
cronStartState.handled = true;
424414
}
425415
},
426416
startChannel,
427417
stopChannel,
428418
getChannelAutostartSuppression: channelManager.getAutostartSuppression,
429419
stopPostReadySidecars: stopRegisteredPostReadySidecars,
430-
reloadPlugins: reloadAttachedGatewayPlugins,
420+
reloadPlugins: kernel.reloadPlugins,
431421
logHooks,
432422
logChannels,
433423
logCron,

0 commit comments

Comments
 (0)