Skip to content

Commit 3dec4e3

Browse files
authored
refactor(gateway): unify maintenance lifecycle ownership (#128766)
1 parent a6a9f55 commit 3dec4e3

8 files changed

Lines changed: 26 additions & 66 deletions

‎config/assertion-safety-baseline.txt‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3052,7 +3052,7 @@ src/gateway/server-plugins.ts 2
30523052
src/gateway/server-reload-restart.ts 2
30533053
src/gateway/server-reload-utils.ts 1
30543054
src/gateway/server-restart-sentinel-agent-delivery.ts 2
3055-
src/gateway/server-runtime-handles.ts 11
3055+
src/gateway/server-runtime-handles.ts 9
30563056
src/gateway/server-runtime-state-prepare.ts 2
30573057
src/gateway/server-session-events.ts 1
30583058
src/gateway/server-startup-bootstrap.ts 1

‎src/gateway/server-close.test.ts‎

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -164,12 +164,16 @@ function createGatewayCloseTestDeps(
164164
stopTaskRegistryMaintenance: null,
165165
nodePresenceTimers: new Map(),
166166
broadcast: vi.fn(),
167-
tickInterval: setInterval(() => undefined, 60_000),
168-
healthInterval: setInterval(() => undefined, 60_000),
169-
dedupeCleanup: setInterval(() => undefined, 60_000),
167+
maintenance: {
168+
tickInterval: setInterval(() => undefined, 60_000),
169+
healthInterval: setInterval(() => undefined, 60_000),
170+
dedupeCleanup: setInterval(() => undefined, 60_000),
171+
startMediaCleanup: vi.fn(),
172+
stopMediaCleanup: vi.fn(async () => "drained" as const),
173+
worktreeCleanup: setInterval(() => undefined, 60_000),
174+
skillCuratorCleanup: vi.fn(),
175+
},
170176
stopMediaCleanup: vi.fn(async () => "drained" as const),
171-
worktreeCleanup: null,
172-
skillCuratorCleanup: vi.fn(),
173177
agentUnsub: null,
174178
taskUnsub: null,
175179
heartbeatUnsub: null,

‎src/gateway/server-close.ts‎

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ import {
3838
} from "./server-chat-state.js";
3939
import type { MediaCleanupStopResult } from "./server-media-cleanup-lifecycle.js";
4040
import { clearSessionTypingState } from "./server-methods/session-typing-state.js";
41+
import type { GatewayMaintenanceHandles } from "./server-runtime-services.js";
4142

4243
const shutdownLog = createSubsystemLogger("gateway/shutdown");
4344
const GATEWAY_SHUTDOWN_HOOK_TIMEOUT_MS = 5_000;
@@ -728,12 +729,8 @@ export function createGatewayCloseHandler(
728729
updateCheckStop?: (() => void) | null;
729730
stopTaskRegistryMaintenance?: (() => Promise<void> | void) | null;
730731
nodePresenceTimers: Map<string, ReturnType<typeof setInterval>>;
731-
tickInterval: ReturnType<typeof setInterval>;
732-
healthInterval: ReturnType<typeof setInterval>;
733-
dedupeCleanup: ReturnType<typeof setInterval>;
732+
maintenance: GatewayMaintenanceHandles | null;
734733
stopMediaCleanup: () => Promise<MediaCleanupStopResult>;
735-
worktreeCleanup: ReturnType<typeof setInterval> | null;
736-
skillCuratorCleanup: () => void;
737734
agentUnsub: (() => Promise<void> | void) | null;
738735
heartbeatUnsub: (() => void) | null;
739736
transcriptUnsub: (() => void) | null;
@@ -976,13 +973,13 @@ export function createGatewayCloseHandler(
976973
reason,
977974
restartExpectedMs,
978975
});
979-
clearInterval(params.tickInterval);
980-
clearInterval(params.healthInterval);
981-
clearInterval(params.dedupeCleanup);
982-
if (params.worktreeCleanup) {
983-
clearInterval(params.worktreeCleanup);
976+
if (params.maintenance) {
977+
clearInterval(params.maintenance.tickInterval);
978+
clearInterval(params.maintenance.healthInterval);
979+
clearInterval(params.maintenance.dedupeCleanup);
980+
clearInterval(params.maintenance.worktreeCleanup);
981+
params.maintenance.skillCuratorCleanup();
984982
}
985-
params.skillCuratorCleanup();
986983
if (params.agentUnsub) {
987984
await shutdownStep("agent-unsub", () => params.agentUnsub!(), warnings);
988985
}

‎src/gateway/server-lifecycle.ts‎

Lines changed: 3 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -324,20 +324,9 @@ export async function prepareGatewayLifecycle(params: {
324324
addGatewayLifetimeSidecar: (sidecar: (typeof runtimeState.gatewayLifetimeSidecars)[number]) => {
325325
runtimeState.gatewayLifetimeSidecars.push(sidecar);
326326
},
327-
setMaintenanceHandles: (handles: {
328-
tickInterval: typeof runtimeState.tickInterval;
329-
healthInterval: typeof runtimeState.healthInterval;
330-
dedupeCleanup: typeof runtimeState.dedupeCleanup;
331-
stopMediaCleanup: typeof runtimeState.stopMediaCleanup;
332-
worktreeCleanup: typeof runtimeState.worktreeCleanup;
333-
skillCuratorCleanup: typeof runtimeState.skillCuratorCleanup;
334-
}) => {
335-
runtimeState.tickInterval = handles.tickInterval;
336-
runtimeState.healthInterval = handles.healthInterval;
337-
runtimeState.dedupeCleanup = handles.dedupeCleanup;
327+
setMaintenanceHandles: (handles: NonNullable<typeof runtimeState.maintenance>) => {
328+
runtimeState.maintenance = handles;
338329
runtimeState.stopMediaCleanup = handles.stopMediaCleanup;
339-
runtimeState.worktreeCleanup = handles.worktreeCleanup;
340-
runtimeState.skillCuratorCleanup = handles.skillCuratorCleanup;
341330
},
342331
};
343332
runtimeState.controlUiSessionPullRequests = createControlUiSessionPullRequestSubscriptions({
@@ -524,12 +513,8 @@ export async function prepareGatewayLifecycle(params: {
524513
stopTaskRegistryMaintenance: shutdownRuntime.stopTaskRegistryMaintenance,
525514
nodePresenceTimers,
526515
broadcast,
527-
tickInterval: runtimeState.tickInterval,
528-
healthInterval: runtimeState.healthInterval,
529-
dedupeCleanup: runtimeState.dedupeCleanup,
516+
maintenance: runtimeState.maintenance,
530517
stopMediaCleanup: stopMediaCleanupForClose,
531-
worktreeCleanup: runtimeState.worktreeCleanup,
532-
skillCuratorCleanup: runtimeState.skillCuratorCleanup,
533518
agentUnsub: runtimeState.agentUnsub,
534519
heartbeatUnsub: runtimeState.heartbeatUnsub,
535520
transcriptUnsub: runtimeState.transcriptUnsub,

‎src/gateway/server-runtime-handles.test.ts‎

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,5 @@ describe("createGatewayServerMutableState", () => {
3030
await vi.advanceTimersByTimeAsync(0);
3131
const restartedState = createGatewayServerMutableState();
3232
await expect(restartedState.stopMediaCleanup()).resolves.toBe("drained");
33-
for (const runtimeState of [state, restartedState]) {
34-
clearInterval(runtimeState.tickInterval);
35-
clearInterval(runtimeState.healthInterval);
36-
clearInterval(runtimeState.dedupeCleanup);
37-
}
3833
});
3934
});

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

Lines changed: 3 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import {
99
waitForMediaCleanupDrains,
1010
} from "./server-media-cleanup-lifecycle.js";
1111
import { createNoopHeartbeatRunner } from "./server-runtime-service-shared.js";
12+
import type { GatewayMaintenanceHandles } from "./server-runtime-services.js";
1213
import type { GatewayPostReadySidecarHandle } from "./server-startup-post-attach.js";
1314

1415
// Mutable server handles track timers, sidecars, subscriptions, and service
@@ -25,12 +26,8 @@ export type GatewayConfigReloaderHandle = {
2526
/** Mutable handles owned by a running gateway server process. */
2627
export type GatewayServerMutableState = {
2728
bonjourStop: (() => Promise<void>) | null;
28-
tickInterval: ReturnType<typeof setInterval>;
29-
healthInterval: ReturnType<typeof setInterval>;
30-
dedupeCleanup: ReturnType<typeof setInterval>;
29+
maintenance: GatewayMaintenanceHandles | null;
3130
stopMediaCleanup: () => Promise<MediaCleanupStopResult>;
32-
worktreeCleanup: ReturnType<typeof setInterval> | null;
33-
skillCuratorCleanup: () => void;
3431
heartbeatRunner: HeartbeatRunner;
3532
stopOutboundDeliveryRecovery: () => Promise<void>;
3633
stopGatewayUpdateCheck: () => void;
@@ -41,7 +38,6 @@ export type GatewayServerMutableState = {
4138
skillsRefreshDelayMs: number;
4239
skillsChangeUnsub: () => Promise<void>;
4340
channelHealthMonitor: ChannelHealthMonitor | null;
44-
mcpServer: { port: number; close: () => Promise<void> } | undefined;
4541
configReloader: GatewayConfigReloaderHandle;
4642
agentUnsub: (() => Promise<void> | void) | null;
4743
heartbeatUnsub: (() => void) | null;
@@ -52,21 +48,10 @@ export type GatewayServerMutableState = {
5248

5349
/** Creates gateway mutable state with inert handles that are safe to stop before startup finishes. */
5450
export function createGatewayServerMutableState(): GatewayServerMutableState {
55-
const noopInterval = () => {
56-
// Dummy unref'd timers give shutdown code a concrete handle to clear even
57-
// when startup exits before real maintenance intervals are installed.
58-
const timer = setInterval(() => {}, 1 << 30);
59-
timer.unref?.();
60-
return timer;
61-
};
6251
return {
6352
bonjourStop: null as (() => Promise<void>) | null,
64-
tickInterval: noopInterval(),
65-
healthInterval: noopInterval(),
66-
dedupeCleanup: noopInterval(),
53+
maintenance: null,
6754
stopMediaCleanup: () => waitForMediaCleanupDrains({ timeoutMs: MEDIA_CLEANUP_STOP_TIMEOUT_MS }),
68-
worktreeCleanup: null as ReturnType<typeof setInterval> | null,
69-
skillCuratorCleanup: () => {},
7055
heartbeatRunner: createNoopHeartbeatRunner(),
7156
stopOutboundDeliveryRecovery: async () => {},
7257
stopGatewayUpdateCheck: () => {},
@@ -77,7 +62,6 @@ export function createGatewayServerMutableState(): GatewayServerMutableState {
7762
skillsRefreshDelayMs: 30_000,
7863
skillsChangeUnsub: async () => {},
7964
channelHealthMonitor: null as ChannelHealthMonitor | null,
80-
mcpServer: undefined as { port: number; close: () => Promise<void> } | undefined,
8165
configReloader: {
8266
stop: async () => {},
8367
notifyPluginMetadataChanged: () => {},

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ export function startGatewayCronWithLogging(params: {
7676
}).catch((err: unknown) => params.logCron.error(`failed to enter start root: ${String(err)}`));
7777
}
7878

79-
async function clearGatewayMaintenanceHandles(
79+
export async function clearGatewayMaintenanceHandles(
8080
maintenance: GatewayMaintenanceHandles | null,
8181
): Promise<void> {
8282
if (!maintenance) {

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

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -448,12 +448,7 @@ export async function finishGatewayStartup(params: {
448448
},
449449
applyMaintenance: async (maintenance) => {
450450
if (lifecycle.closePreludeStarted) {
451-
clearInterval(maintenance.tickInterval);
452-
clearInterval(maintenance.healthInterval);
453-
clearInterval(maintenance.dedupeCleanup);
454-
await maintenance.stopMediaCleanup();
455-
clearInterval(maintenance.worktreeCleanup);
456-
maintenance.skillCuratorCleanup();
451+
await gatewayRuntimeServices.clearGatewayMaintenanceHandles(maintenance);
457452
return;
458453
}
459454
// Publish the stop owner before cleanup can touch SQLite or state paths;

0 commit comments

Comments
 (0)