Skip to content

Soul-driven AI agent with permission-hardened tools, token budgets, and multi-channel access. Runs 24/7 from CLI, Telegram or More.

fix(plugins): installing into a running Mercury never started the plugin

Salman Qureshi3e45646156f6
diff --git a/src/plugins/manager.ts b/src/plugins/manager.ts
index 9943f6a2..9e3c0bc8 100644
--- a/src/plugins/manager.ts
+++ b/src/plugins/manager.ts
@@ -75,6 +75,8 @@ export class PluginManager {
private readonly safeMode: boolean;
private notifyFn: (message: string) => void | Promise<void>;
private reconciling: Promise<void> | null = null;
+ private reconcileAgain = false;
+ private passSeq = 0;
private healthyTimer: ReturnType<typeof setTimeout> | null = null;
private booted = false;
private runs: boolean;
@@ -185,25 +187,43 @@ export class PluginManager {
/** Make the running set match state.json. Serialised; never throws. */
async reconcile(): Promise<void> {
if (this.safeMode || !this.runs) return;
+ // A request that arrives mid-pass may be for a change that pass already
+ // read past (e.g. an install): ask for one more pass instead of reusing it.
+ this.reconcileAgain = true;
if (this.reconciling) return this.reconciling;
- this.reconciling = (async () => {
+ const passId = ++this.passSeq;
+ const pass = (async () => {
+ // Yield first: a pass with nothing to await must not finish before
+ // `this.reconciling` is assigned, or the settled promise would stick
+ // there and every later reconcile would silently do nothing.
+ await Promise.resolve();
try {
- const state = readState();
- for (const [id, run] of [...this.running]) {
- const entry = state.plugins[id];
- if (!entry || !entry.enabled || entry.version !== run.version) await this.stopRuntime(id);
+ while (this.reconcileAgain) {
+ this.reconcileAgain = false;
+ await this.reconcileOnce();
}
- for (const [id, entry] of Object.entries(state.plugins)) {
- if (entry.enabled && !this.running.has(id)) await this.startRuntime(id, entry.version);
- }
- } catch (err) {
- logger.error({ err }, 'Plugin reconcile failed');
} finally {
- this.reconciling = null;
+ if (this.passSeq === passId) this.reconciling = null;
this.changed();
}
})();
- return this.reconciling;
+ this.reconciling = pass;
+ return pass;
+ }
+
+ private async reconcileOnce(): Promise<void> {
+ try {
+ const state = readState();
+ for (const [id, run] of [...this.running]) {
+ const entry = state.plugins[id];
+ if (!entry || !entry.enabled || entry.version !== run.version) await this.stopRuntime(id);
+ }
+ for (const [id, entry] of Object.entries(state.plugins)) {
+ if (entry.enabled && !this.running.has(id)) await this.startRuntime(id, entry.version);
+ }
+ } catch (err) {
+ logger.error({ err }, 'Plugin reconcile failed');
+ }
}
private async startRuntime(id: string, version: string): Promise<void> {
diff --git a/src/plugins/reconcile.test.ts b/src/plugins/reconcile.test.ts
new file mode 100644
index 00000000..4e90c2fd
--- /dev/null
+++ b/src/plugins/reconcile.test.ts
@@ -0,0 +1,34 @@
+import { mkdirSync } from 'node:fs';
+import { join } from 'node:path';
+import { afterEach, beforeEach, expect, it } from 'vitest';
+import { PluginManager } from './manager.js';
+import { outOfProcessPlugin, tempHome } from './test-fixtures.js';
+import { resetToolBridge } from './tool-bridge.js';
+
+let env: ReturnType<typeof tempHome>;
+let mgr: PluginManager;
+beforeEach(() => { env = tempHome(); resetToolBridge(); mgr = new PluginManager({ safeMode: false, notify: () => {} }); });
+afterEach(async () => { await mgr.shutdown(); env.restore(); });
+
+// Regression: boot() with nothing installed left a settled promise in
+// `reconciling`, so a later install into the running daemon never started.
+it('install into a running manager starts it (fresh, and after uninstall)', async () => {
+ const src = join(env.home, 'src'); mkdirSync(src, { recursive: true });
+ await mgr.boot();
+ const dir = outOfProcessPlugin(src, { id: 'oop' });
+ await mgr.install(dir);
+ expect((await mgr.info('oop'))?.running).toBe(true);
+ await mgr.uninstall('oop');
+ await mgr.install(dir);
+ expect((await mgr.info('oop'))?.running).toBe(true);
+});
+
+it('a reconcile requested mid-pass runs again and sees the change', async () => {
+ const src = join(env.home, 'src'); mkdirSync(src, { recursive: true });
+ await mgr.boot();
+ const a = outOfProcessPlugin(src, { id: 'oop-a' });
+ const b = outOfProcessPlugin(src, { id: 'oop-b' });
+ await Promise.all([mgr.install(a), mgr.install(b)]);
+ expect((await mgr.info('oop-a'))?.running).toBe(true);
+ expect((await mgr.info('oop-b'))?.running).toBe(true);
+});