diff --git a/src/gateway/server-close.ts b/src/gateway/server-close.ts index 06c16e12844..51f3fa6d1cc 100644 --- a/src/gateway/server-close.ts +++ b/src/gateway/server-close.ts @@ -5,6 +5,7 @@ import { disposeAllSessionMcpRuntimes } from "../agents/pi-bundle-mcp-tools.js"; import type { CanvasHostHandler, CanvasHostServer } from "../canvas-host/server.js"; import { type ChannelId, listChannelPlugins } from "../channels/plugins/index.js"; import { stopGmailWatcher } from "../hooks/gmail-watcher.js"; +import { createInternalHookEvent, triggerInternalHook } from "../hooks/internal-hooks.js"; import type { HeartbeatRunner } from "../infra/heartbeat-runner.js"; import { createSubsystemLogger } from "../logging/subsystem.js"; import type { PluginServicesHandle } from "../plugins/services.js"; @@ -113,6 +114,27 @@ export function createGatewayCloseHandler(params: { typeof opts?.restartExpectedMs === "number" && Number.isFinite(opts.restartExpectedMs) ? Math.max(0, Math.floor(opts.restartExpectedMs)) : null; + try { + const shutdownEvent = createInternalHookEvent("gateway", "shutdown", "gateway:shutdown", { + reason, + restartExpectedMs, + }); + await triggerInternalHook(shutdownEvent); + if (restartExpectedMs !== null) { + const preRestartEvent = createInternalHookEvent( + "gateway", + "pre-restart", + "gateway:pre-restart", + { + reason, + restartExpectedMs, + }, + ); + await triggerInternalHook(preRestartEvent); + } + } catch { + // Best-effort only; shutdown should proceed even if hooks fail. + } if (params.bonjourStop) { try { await params.bonjourStop();