fix(plugins): reap a queued erasure only once the plugin's data is gone

The orphan reap deleted every queue row whose plugin had left the registry, but
uninstall(deleteData=false) removes the plugins row while deliberately keeping the
data dir AND the queued erasure so a same-id reinstall can still honour it. The reap
now deletes a row only when the plugin's data dir is actually gone; a deleteData=true
uninstall already clears the rows itself.
This commit is contained in:
Maurice
2026-07-11 21:52:52 +02:00
parent ffa1af61b7
commit 512fc51f3e
2 changed files with 39 additions and 5 deletions
@@ -21,7 +21,7 @@ import { discoverPlugins } from './install/discovery';
import { parseJsonText, parseManifest } from './install/manifest';
import { scanForNativeBinaries } from './install/native-scan';
import { devLinkEnabled, DEV_LINK_SOURCE } from './dev-link';
import { pluginCodeDir } from './paths';
import { pluginCodeDir, pluginDataDir } from './paths';
import { PluginRegistryService } from './registry/registry.service';
import { isAddonEnabled } from '../../services/adminService';
import type { PluginDependency } from './install/manifest';
@@ -281,10 +281,21 @@ export class PluginRuntimeService implements OnModuleInit, OnModuleDestroy {
private async runDrainOnce(): Promise<void> {
if (!pluginsEnabled()) return;
try {
// Reap rows whose plugin no longer exists (uninstalled): its data dir was removed
// with it, so the erasure is already satisfied, and the plugin will never reactivate
// to ACK the row — left alone it would linger in the queue forever.
db.prepare('DELETE FROM plugin_user_erasure_queue WHERE plugin_id NOT IN (SELECT id FROM plugins)').run();
// Reap a queued erasure only when its plugin is gone from the registry AND its data
// dir is actually deleted — then the data (and the obligation) is truly gone. A plugin
// uninstalled with "keep data" (deleteData=false) removes the plugins row but
// DELIBERATELY keeps the data dir and the queue row so a same-id reinstall can still
// honour the erasure (see uninstall()); reaping on registry-absence alone would wipe
// exactly those preserved obligations. A deleteData=true uninstall already clears the
// rows itself, so this only ever needs to catch a truly orphaned data dir.
const orphans = db
.prepare('SELECT DISTINCT plugin_id FROM plugin_user_erasure_queue WHERE plugin_id NOT IN (SELECT id FROM plugins)')
.all() as Array<{ plugin_id: string }>;
for (const { plugin_id } of orphans) {
if (!fs.existsSync(pluginDataDir(plugin_id))) {
db.prepare('DELETE FROM plugin_user_erasure_queue WHERE plugin_id = ?').run(plugin_id);
}
}
// Only ACTIVE plugins can be delivered to; scope the window to them so a backlog
// of erasures for permanently-inactive plugins can't starve deliverable ones.
const active = this.supervisor.activeIds();
@@ -194,6 +194,29 @@ describe('PluginRuntimeService (M2 end-to-end)', () => {
await runtime.deactivate('gdpr');
});
it('the drain reap keeps a queued erasure while the plugin data dir survives (uninstall keep-data), reaping only once the data is gone', async () => {
// uninstall(deleteData=false) removes the plugins row but DELIBERATELY keeps the data
// dir and the queued erasure so a same-id reinstall can still honour it. The orphan
// reap must not delete that row while the data dir is still present.
const id = 'reaptest';
const dir = path.join(dataRoot, id);
fs.mkdirSync(dir, { recursive: true });
fs.writeFileSync(path.join(dir, 'plugin.db'), 'x');
testDb.prepare('INSERT INTO plugin_user_erasure_queue (plugin_id, user_id) VALUES (?, ?)').run(id, 9);
// the plugin is gone from the registry (uninstalled) but its data dir remains
expect(testDb.prepare('SELECT id FROM plugins WHERE id = ?').get(id)).toBeUndefined();
// eslint-disable-next-line @typescript-eslint/no-explicit-any
await (runtime as any).drainUserErasures();
expect((testDb.prepare("SELECT COUNT(*) c FROM plugin_user_erasure_queue WHERE plugin_id='reaptest'").get() as { c: number }).c).toBe(1);
// now the data is truly gone (deleteData=true / manual removal) → the row is reaped
fs.rmSync(dir, { recursive: true, force: true });
// eslint-disable-next-line @typescript-eslint/no-explicit-any
await (runtime as any).drainUserErasures();
expect((testDb.prepare("SELECT COUNT(*) c FROM plugin_user_erasure_queue WHERE plugin_id='reaptest'").get() as { c: number }).c).toBe(0);
});
it('leaves an erasure queued for a plugin that is offline, to run when it is back', async () => {
// granted the hook but NOT active → enqueue keeps the row, drain leaves it
testDb.prepare("INSERT INTO plugins (id, status, permissions, config) VALUES ('offliner','inactive','[\"db:own\",\"hook:user-data\"]','{}')").run();