The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
feat(desktop): plugin ctx.onDispose + self-disposing kanban bindApi
Brooklyn Nicholson77f1e84a3492
diff --git a/apps/desktop/src/contrib/plugin.test.ts b/apps/desktop/src/contrib/plugin.test.tsnew file mode 100644index 00000000000..ca32f49e891--- /dev/null+++ b/apps/desktop/src/contrib/plugin.test.ts@@ -0,0 +1,21 @@+import { describe, expect, it } from 'vitest'++import { createPluginContext } from './plugin'++describe('createPluginContext.onDispose', () => {+ it('collects arbitrary cleanups so the host runs them on deactivate', () => {+ const disposers: Array<() => void> = []+ const ctx = createPluginContext('demo', dispose => disposers.push(dispose))++ let cleaned = false+ ctx.onDispose(() => {+ cleaned = true+ })++ // The cleanup is tracked alongside contribution/socket disposers, so the+ // loader's deactivate (which runs every collected disposer) tears it down.+ expect(disposers).toHaveLength(1)+ disposers.forEach(dispose => dispose())+ expect(cleaned).toBe(true)+ })+})diff --git a/apps/desktop/src/contrib/plugin.ts b/apps/desktop/src/contrib/plugin.tsindex 551f626e62f..a88aa078823 100644--- a/apps/desktop/src/contrib/plugin.ts+++ b/apps/desktop/src/contrib/plugin.ts@@ -40,6 +40,10 @@ export interface PluginContext {register: (c: PluginContribution) => () => void/** Register several at once; the returned disposer removes all of them. */registerMany: (cs: PluginContribution[]) => () => void+ /** Register an arbitrary cleanup to run on unload/disable — for side effects+ * that aren't contributions or sockets (store subscriptions, timers). Runs+ * alongside every other disposer when the plugin deactivates. */+ onDispose: (fn: () => void) => void/** REST to this plugin's own backend namespace (`/api/plugins/<id>`); `path`* is relative ('/board'). The sanctioned door for a plugin that ships a* `plugin_api.py` — profile-aware, namespace-scoped by construction. Use@@ -108,6 +112,7 @@ export function createPluginContext(pluginId: string, onDispose?: (dispose: () =source,register: c => track(registry.register(scope(c))),registerMany: cs => track(registry.registerMany(cs.map(scope))),+ onDispose: fn => void track(fn),rest: <T>(path: string, opts?: PluginRestOptions) => pluginRest<T>(pluginId, path, opts),socket: (path, onMessage) => track(pluginSocket(pluginId, path, onMessage)),storage: createPluginStorage(pluginId),diff --git a/apps/desktop/src/plugins/kanban/api.ts b/apps/desktop/src/plugins/kanban/api.tsindex 9a1a9b294a7..16403bbc4ee 100644--- a/apps/desktop/src/plugins/kanban/api.ts+++ b/apps/desktop/src/plugins/kanban/api.ts@@ -67,18 +67,32 @@ function onEventsFrame(slug: string, data: unknown): void {}}-/** Bind the plugin's doors once, at register time. The events socket is pinned- * to a board at handshake, so a board switch closes + reopens it. */-export function bindApi(r: Rest, storage: PluginStorage, socket: Socket): void {+// A persisted, subscribable atom (the structural slice we need — avoids+// importing nanostore's type just to describe one).+interface Persisted<T> {+ get(): T+ set(value: T): void+ listen(cb: (value: T) => void): () => void+}++/** Bind the plugin's doors at register time and return a disposer the host+ * runs on unload/disable — so nothing (store sync, socket) survives a toggle+ * or duplicates on re-enable. The events socket is pinned to a board at+ * handshake, so a board switch closes + reopens it. */+export function bindApi(r: Rest, storage: PluginStorage, socket: Socket): () => void {rest = r- $boardSlug.set(storage.get(BOARD_SLUG_KEY, ''))- $boardSlug.listen(slug => storage.set(BOARD_SLUG_KEY, slug))- $introDismissed.set(storage.get(INTRO_KEY, false))- $introDismissed.listen(dismissed => storage.set(INTRO_KEY, dismissed))- $lanesByProfile.set(storage.get(LANES_KEY, false))- $lanesByProfile.listen(on => storage.set(LANES_KEY, on))- $collapsedLanes.set(storage.get(COLLAPSED_KEY, {}))- $collapsedLanes.listen(map => storage.set(COLLAPSED_KEY, map))+ const unsubs: Array<() => void> = []++ // Hydrate an atom from storage and keep storage in sync with it.+ const persist = <T>(atom: Persisted<T>, key: string, fallback: T) => {+ atom.set(storage.get(key, fallback))+ unsubs.push(atom.listen(value => storage.set(key, value)))+ }++ persist($boardSlug, BOARD_SLUG_KEY, '')+ persist($introDismissed, INTRO_KEY, false)+ persist($lanesByProfile, LANES_KEY, false)+ persist($collapsedLanes, COLLAPSED_KEY, {})let close: (() => void) | null = null@@ -88,7 +102,13 @@ export function bindApi(r: Rest, storage: PluginStorage, socket: Socket): void {}open($boardSlug.get())- $boardSlug.listen(open)+ unsubs.push($boardSlug.listen(open))++ return () => {+ unsubs.forEach(unsub => unsub())+ close?.()+ rest = null+ }}function call<T>(path: string, opts?: PluginRestOptions): Promise<T> {diff --git a/apps/desktop/src/plugins/kanban/plugin.tsx b/apps/desktop/src/plugins/kanban/plugin.tsxindex c368e65aeae..227d409655e 100644--- a/apps/desktop/src/plugins/kanban/plugin.tsx+++ b/apps/desktop/src/plugins/kanban/plugin.tsx@@ -79,7 +79,7 @@ const plugin: HermesPlugin = {name: 'Kanban',defaultEnabled: false,register(ctx) {- bindApi(ctx.rest, ctx.storage, ctx.socket)+ ctx.onDispose(bindApi(ctx.rest, ctx.storage, ctx.socket))ctx.registerMany([{