import { discoverPlugins } from './commandRouter.js'; import { loadPluginApi } from './pluginLoader.js'; import type { PluginRuntimeContext } from './pluginApi.js'; export async function activatePlugins( ctx: PluginRuntimeContext, ): Promise<() => void> { const manifests = await discoverPlugins(); const cleanups: Array<() => void> = []; for (const manifest of manifests) { if (ctx.signal?.aborted) break; const api = await loadPluginApi(manifest.root); if (!api?.activate || ctx.signal?.aborted) continue; try { const cleanup = await api.activate(ctx); if (typeof cleanup === 'function') cleanups.push(cleanup); } catch (err) { console.warn( `[minimal-agent] plugin activate failed (${manifest.name}): ${ err instanceof Error ? err.message : String(err) }`, ); } } return () => { // Drain before calling: teardown is idempotent, including reentrant hosts. for (const cleanup of cleanups.splice(0).reverse()) { try { cleanup(); } catch { } } }; }