feat(FN-3722): route plugin store to configured global directory

Merges centralized plugin installation (FN-3722): TaskStore plugin store is now routed to a configured global directory with legacy migration hardening, and CLI plugin commands are aligned accordingly, backed by comprehensive integration and regression tests. Also includes a dashboard fix to return

Fusion-Task-Id: FN-3722
This commit is contained in:
Fusion
2026-05-08 08:03:39 -07:00
committed by gsxdsm
parent 6797cc23ba
commit b8d14849ee
17 changed files with 351 additions and 42 deletions

View File

@@ -1,6 +1,7 @@
import { describe, it, expect, beforeEach, afterEach, vi } from "vitest";
import { PluginStore } from "../plugin-store.js";
import { Database, toJson } from "../db.js";
import { CentralDatabase } from "../central-db.js";
import { rm } from "node:fs/promises";
import { join } from "node:path";
import { mkdtempSync } from "node:fs";
@@ -142,7 +143,7 @@ describe("PluginStore", () => {
}
});
it("is idempotent when init runs multiple times", async () => {
it("is idempotent across repeated init and store rehydration", async () => {
const migrationProject = makeTmpDir();
const migrationCentral = makeTmpDir();
try {
@@ -157,14 +158,59 @@ describe("PluginStore", () => {
await migrationStore.init();
await migrationStore.init();
const plugins = await migrationStore.listPlugins();
const reopenedStore = new PluginStore(migrationProject, { centralGlobalDir: migrationCentral });
await reopenedStore.init();
const plugins = await reopenedStore.listPlugins();
expect(plugins.filter((plugin) => plugin.id === "legacy-idempotent")).toHaveLength(1);
const centralDb = new CentralDatabase(migrationCentral);
centralDb.init();
const installCount = centralDb
.prepare("SELECT COUNT(*) as count FROM plugin_installs WHERE id = ?")
.get("legacy-idempotent") as { count: number };
expect(installCount.count).toBe(1);
const localDb = new Database(join(migrationProject, ".fusion"));
localDb.init();
const marker = localDb
.prepare("SELECT value FROM __meta WHERE key = 'pluginCentralMigrationV1'")
.get() as { value: string } | undefined;
expect(marker?.value).toBe("done");
} finally {
await rm(migrationProject, { recursive: true, force: true });
await rm(migrationCentral, { recursive: true, force: true });
}
});
it("shows globally installed plugin in another project as disabled until explicitly enabled", async () => {
const projectA = makeTmpDir();
const projectB = makeTmpDir();
const sharedCentral = makeTmpDir();
try {
const storeA = new PluginStore(projectA, { centralGlobalDir: sharedCentral });
const storeB = new PluginStore(projectB, { centralGlobalDir: sharedCentral });
await storeA.init();
await storeB.init();
await storeA.registerPlugin({
manifest: makeManifest({ id: "shared-global", name: "Shared Global" }),
path: "/plugins/shared-global",
});
const inProjectB = await storeB.getPlugin("shared-global");
expect(inProjectB.enabled).toBe(false);
await storeB.enablePlugin("shared-global");
const enabledInProjectB = await storeB.getPlugin("shared-global");
expect(enabledInProjectB.enabled).toBe(true);
} finally {
await rm(projectA, { recursive: true, force: true });
await rm(projectB, { recursive: true, force: true });
await rm(sharedCentral, { recursive: true, force: true });
}
});
it("keeps latest updatedAt install metadata across projects while preserving per-project enablement", async () => {
const projectA = makeTmpDir();
const projectB = makeTmpDir();

View File

@@ -25,6 +25,7 @@ const mockedRunCommandAsync = vi.mocked(runCommandAsync);
import { TaskStore, TaskHasDependentsError } from "../store.js";
import { AgentStore } from "../agent-store.js";
import { CentralDatabase } from "../central-db.js";
import { appendFile, readFile, writeFile, mkdir, rm, readdir, unlink } from "node:fs/promises";
import { join } from "node:path";
import { mkdtempSync, existsSync } from "node:fs";
@@ -134,6 +135,37 @@ describe("TaskStore", () => {
`).run(taskId, timestamp, text, type, detail ?? null, agent ?? null);
}
describe("plugin store routing", () => {
it("routes plugin writes to the configured central global dir", async () => {
const pluginStore = store.getPluginStore();
await pluginStore.init();
await pluginStore.registerPlugin({
manifest: {
id: "taskstore-plugin",
name: "TaskStore Plugin",
version: "1.0.0",
},
path: "/tmp/taskstore-plugin",
});
const centralDb = new CentralDatabase(globalDir);
centralDb.init();
const installCount = centralDb
.prepare("SELECT COUNT(*) as count FROM plugin_installs WHERE id = ?")
.get("taskstore-plugin") as { count: number };
expect(installCount.count).toBe(1);
const localCount = store
.getDatabase()
.prepare("SELECT COUNT(*) as count FROM plugins WHERE id = ?")
.get("taskstore-plugin") as { count: number };
expect(localCount.count).toBe(0);
centralDb.close();
});
});
// ── Prompt generation (no duplicate description) ───────────────
describe("prompt generation", () => {

View File

@@ -1635,6 +1635,10 @@ export class Database {
if (version < 24) {
this.applyMigration(24, () => {
// Legacy project-local plugin table (introduced in v24) is retained for
// one-shot migration reads by PluginStore.migrateLegacyProjectRows().
// Post-FN-3722 all new plugin install writes must go to central
// plugin_installs + project_plugin_states tables; writes here are a bug.
this.db.exec(`
CREATE TABLE IF NOT EXISTS plugins (
id TEXT PRIMARY KEY,

View File

@@ -342,11 +342,11 @@ export class PluginStore extends EventEmitter<PluginStoreEvents> {
row.updatedAt,
);
}
this.localDb
.prepare("INSERT INTO __meta (key, value) VALUES ('pluginCentralMigrationV1', 'done') ON CONFLICT(key) DO UPDATE SET value = excluded.value")
.run();
});
this.localDb
.prepare("INSERT INTO __meta (key, value) VALUES ('pluginCentralMigrationV1', 'done') ON CONFLICT(key) DO UPDATE SET value = excluded.value")
.run();
}
async registerPlugin(input: PluginRegistrationInput): Promise<PluginInstallation> {

View File

@@ -550,6 +550,7 @@ export class TaskStore extends EventEmitter<TaskStoreEvents> {
// Tests that need cross-instance persistence (open store A, close,
// open store B on the same dir, expect data) must leave this false.
private readonly inMemoryDb: boolean;
private readonly globalSettingsDir?: string;
constructor(
private rootDir: string,
@@ -565,6 +566,7 @@ export class TaskStore extends EventEmitter<TaskStoreEvents> {
this.inMemoryDb = options?.inMemoryDb === true;
const resolvedGlobalSettingsDir = globalSettingsDir
?? (process.env.VITEST === "true" ? join(rootDir, ".fusion-global-settings") : undefined);
this.globalSettingsDir = resolvedGlobalSettingsDir;
this.globalSettingsStore = new GlobalSettingsStore(resolvedGlobalSettingsDir);
}
@@ -6804,7 +6806,9 @@ ${notificationsSection}`;
*/
getPluginStore(): PluginStore {
if (!this.pluginStore) {
this.pluginStore = new PluginStore(this.rootDir);
// PluginStore persists install/state rows in central DB, so it must use
// the same resolved global settings directory as TaskStore.
this.pluginStore = new PluginStore(this.rootDir, { centralGlobalDir: this.globalSettingsDir });
}
return this.pluginStore;
}