Skip to content

The agent that grows with you

fix(desktop): dispose runtime plugin event listeners

KoNit-K14b2ad159692
diff --git a/apps/desktop/src/contrib/events.test.ts b/apps/desktop/src/contrib/events.test.ts
new file mode 100644
index 00000000000..b3cba02739f
--- /dev/null
+++ b/apps/desktop/src/contrib/events.test.ts
@@ -0,0 +1,17 @@
+import { describe, expect, it, vi } from 'vitest'
+
+import { emitGatewayEvent, onGatewayEvent } from './events'
+
+describe('onGatewayEvent', () => {
+ it('keeps the legacy direct subscription and disposer behavior outside plugin registration', () => {
+ const listener = vi.fn()
+ const dispose = onGatewayEvent('gateway.ready', listener)
+
+ emitGatewayEvent({ type: 'gateway.ready' } as never)
+ expect(listener).toHaveBeenCalledTimes(1)
+
+ dispose()
+ emitGatewayEvent({ type: 'gateway.ready' } as never)
+ expect(listener).toHaveBeenCalledTimes(1)
+ })
+})
diff --git a/apps/desktop/src/contrib/events.ts b/apps/desktop/src/contrib/events.ts
index ef0f6897d6e..202cc755e1e 100644
--- a/apps/desktop/src/contrib/events.ts
+++ b/apps/desktop/src/contrib/events.ts
@@ -11,6 +11,24 @@ import type { GatewayEvent } from '@hermes/shared'
export type GatewayEventListener = (event: GatewayEvent) => void
const listeners = new Map<string, Set<GatewayEventListener>>()
+let activeDisposerTracker: ((dispose: () => void) => void) | null = null
+
+/** Run a plugin registration while associating its event listeners with its
+ * unload disposer list. Runtime plugins are evaluated afresh on every reload,
+ * so their host subscriptions must leave with the previous module instance. */
+export function trackGatewayEventDisposers<T>(
+ track: (dispose: () => void) => void,
+ register: () => T
+): T {
+ const previous = activeDisposerTracker
+ activeDisposerTracker = track
+
+ try {
+ return register()
+ } finally {
+ activeDisposerTracker = previous
+ }
+}
/** Subscribe to gateway events by `type` (`'*'` = everything). Returns a disposer. */
export function onGatewayEvent(type: string, listener: GatewayEventListener): () => void {
@@ -18,13 +36,17 @@ export function onGatewayEvent(type: string, listener: GatewayEventListener): ()
set.add(listener)
listeners.set(type, set)
- return () => {
+ const dispose = () => {
set.delete(listener)
if (set.size === 0) {
listeners.delete(type)
}
}
+
+ activeDisposerTracker?.(dispose)
+
+ return dispose
}
/** Fan an event to subscribers (wiring-side; call before app dispatch). */
diff --git a/apps/desktop/src/contrib/runtime-loader.test.ts b/apps/desktop/src/contrib/runtime-loader.test.ts
index f1ea3d331c9..68ef06cef9c 100644
--- a/apps/desktop/src/contrib/runtime-loader.test.ts
+++ b/apps/desktop/src/contrib/runtime-loader.test.ts
@@ -4,7 +4,8 @@ import type { HermesReadDirResult } from '@/global'
import type * as HermesModule from '@/hermes'
import { $pluginRecords, publishPlugin, setPluginEnabled } from './plugins-store'
-import { discoverRuntimePlugins, loadRuntimePlugin, watchRuntimePlugins } from './runtime-loader'
+import { emitGatewayEvent } from './events'
+import { discoverRuntimePlugins, loadRuntimePlugin, unloadRuntimePlugin, watchRuntimePlugins } from './runtime-loader'
// getStatus would supply the connected backend's hermes_home — a REMOTE path in
// remote mode. The disk scanner must NOT derive the plugin root from it (#66899).
@@ -349,6 +350,38 @@ describe('plugin source reads (512 KiB preview-cap bug)', () => {
delete (globalThis as unknown as { __smallRegister?: unknown }).__smallRegister
}
})
+
+ it('disposes runtime host event subscriptions before a hot reload (#112366)', async () => {
+ const restore = blobToDataUrl()
+ const marker = '__runtimeEventReloadCount'
+ ;(globalThis as unknown as Record<string, number>)[marker] = 0
+
+ try {
+ const source = `
+ import { host } from '@hermes/plugin-sdk'
+ export default {
+ id: 'runtime-event-reload',
+ register() {
+ host.onEvent('bot_relay.outbox.pending', () => { globalThis.${marker}++ })
+ }
+ }
+ `
+
+ await loadRuntimePlugin(source, 'first runtime event registration')
+ await loadRuntimePlugin(source, 'second runtime event registration')
+
+ emitGatewayEvent({ type: 'bot_relay.outbox.pending' } as never)
+ expect((globalThis as unknown as Record<string, number>)[marker]).toBe(1)
+
+ unloadRuntimePlugin('runtime-event-reload')
+ emitGatewayEvent({ type: 'bot_relay.outbox.pending' } as never)
+ expect((globalThis as unknown as Record<string, number>)[marker]).toBe(1)
+ } finally {
+ unloadRuntimePlugin('runtime-event-reload')
+ delete (globalThis as unknown as Record<string, number | undefined>)[marker]
+ restore()
+ }
+ })
})
describe('bundled-shadowed disk copies', () => {
diff --git a/apps/desktop/src/contrib/runtime-loader.ts b/apps/desktop/src/contrib/runtime-loader.ts
index 80acab45160..2bd7a03d22a 100644
--- a/apps/desktop/src/contrib/runtime-loader.ts
+++ b/apps/desktop/src/contrib/runtime-loader.ts
@@ -33,6 +33,7 @@ import { atom } from 'nanostores'
import { installPluginSdk, sdkImportMap } from '@/sdk/runtime'
import { notifyError } from '@/store/notifications'
+import { trackGatewayEventDisposers } from './events'
import { createPluginContext, type HermesPlugin } from './plugin'
import { $pluginRecords, dropPlugin, pluginActive, type PluginKind, publishPlugin } from './plugins-store'
@@ -183,7 +184,10 @@ export async function loadRuntimePlugin(
// Reload = dispose the previous incarnation, then register fresh.
unloadRuntimePlugin(plugin.id)
const disposers: (() => void)[] = []
- plugin.register(createPluginContext(plugin.id, dispose => disposers.push(dispose)))
+ trackGatewayEventDisposers(
+ dispose => disposers.push(dispose),
+ () => plugin.register(createPluginContext(plugin.id, dispose => disposers.push(dispose)))
+ )
loaded.set(plugin.id, disposers)
publishPlugin({ ...record, status: 'loaded' })
}